Das Ziel meines Codes ist für den Benutzer, den Satz einzugeben, nach einer Position zu fragen und dann das Ganze in eine/zwei Dateien zu lesen, dh die Positionen und die Wörter.Warum erhalte ich den Attributfehler?
sentencelist=[] #variable list for the sentences
word=[] #variable list for the words
positionofword=[]
words= open("words.txt","w")
position= open("position.txt","w")
question=input("Do you want to enter a sentence? Answers are Y or N.").upper()
if question=="Y":
sentence=input("Please enter a sentance").upper() #sets to uppercase so it's easier to read
sentencetext=sentence.isalpha or sentence.isspace()
while sentencetext==False: #if letters have not been entered
print("Only letters are allowed") #error message
sentence=input("Please enter a sentence").upper() #asks the question again
sentencetext=sentence.isalpha #checks if letters have been entered this time
elif question=="N":
print("The program will now close")
else:
print("please enter a letter")
sentence_word = sentence.split(' ')
for (i, check) in enumerate(word): #orders the words
print(sentence)
sentence_words = sentence.split(' ')
word = input("What word are you looking for?").upper() #asks what word they want
for (i, check) in enumerate(sentence_words): #orders the words
if (check == word):
positionofword=print(str(("your word is in this position:", i+1)))
positionofword=i+1
break
else:
print("This didn't work")
words.write(word + " ")
position.write(positionofword + " ")
words.close()
position.close()
, dass mein Code ist, aber ich erhalte diesen Fehler
position.write(positionofword + " ")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Beachten Sie, dass die Word-Datei als auch leer.
in Ihrer for-Schleife Sie Neuzuweisung 'positionofword' bis i + 1, was es für eine ganze Zahl macht. Sie können den '+' Operator an einem int und einer Zeichenkette nicht benutzen – Simon
Simon also nehme ich das 'postionofword = i + 1' heraus? – hana