Jemand gab mir diesen Code zu versuchen, zu beheben, (Schwein Latein) Ich habe es, um eine Zeile durch eine TXT-Datei zu akzeptieren, aber wenn ich weitere Zeilen im TXT hinzufügen Datei bricht ab. Mit Indexfehler. Jeder von euch weiß, was mein Problem ist?Python 2.7 - Akzeptieren mehrere Zeilen durch eine TXT-Datei
vowels = ("A", "a", "E", "e", "I", "i", "O", "o", "U", "u")
# Functions
text = raw_input("Enter the path of a text file: ")
def pig_word(string):
for line in text:
line = line.split()
lines = []
for line in string.split('\n'):
new_string = ""
for word in line.split(" "):
first_letter = word[0]
if first_letter in vowels:
new_string += word + "way" + " "
else:
new_string += word[1:] + first_letter + "ay" + " "
global new_string
lines.append(new_string)
def line_counter(s):
line_count = 0
for _ in s.split("\n"):
line_count += 1
return line_count
def word_counter(line):
word_count = 0
list_of_words = line.split()
word_count += len(list_of_words)
return word_count
# File path conversion
file_path = open(text, "r")
out_file = open("pig_output.txt", "w")
s = file_path.read()
pig = pig_word(s)
out_file.write(str(new_string)+ "\n")
out_file.write("\n")
linecount = line_counter(s)
wordcount = word_counter(s)
file_path.close()
out_file.close()
# Results
print "\n\n\n\nTranslation finished and written to pig_output.txt"
print "A total of {} lines were translated successfully.".format(linecount)
print "A total of {} words were translated successfully.".format(wordcount)
print "\n\n\n\n"
Fehler ist:
Traceback (most recent call last):
File "C:/Users/Administrator.MU-L-ZB046882H/Desktop/Code/chal.py", line 42, in <module>
pig = pig_word(s)
File "C:/Users/Administrator.MU-L-ZB046882H/Desktop/Code/chal.py", line 13, in pig_word
first_letter = word[0]
IndexError: string index out of range
>>>
Sieht aus, als hätte jemand dir Hausaufgaben gemacht. Gut. Es würde helfen, wenn Sie genaue Fehlermeldung bieten, enthält es oft eine Menge nützlicher Informationen (wie genaue Anweisung, Codezeile, etc.) –
Oh, vergessen Sie, dass danke zu addieren! Weiß nicht, ob es Hausaufgaben ist oder nicht . Nur eine Herausforderung, die mir jemand gab. – PaulB1234
War diese Person der Fragesteller von [dieser Frage] (http://stackoverflow.com/q/36666848/2564301)? – usr2564301