1

I've looked at several other posts asking this same question but none of them seem to apply to me. Here's the code:

wordNumber = input("Word Number: ")
addedWords = 0  
wordList = []


while addedWords != wordNumber:
    Word = input("Word: ")
    wordList.append(Word)
    addedWords = addedWords + 1

I've been setting wordNumber to equal 5. I would think that since I am adding 1 to addedWordsin each loop, it should work perfectly and stop looping once addedWords is equal to wordNumber. I can't even fathom what I could be missing here.

Thank you!

2 Answers 2

8

input returns a string; string cannot be equal to int:

>>> '5' == 5   # str <-> int
False

>>> int('5') == 5  # int <-> int
True

You need to convert the string to int:

wordNumber = int(input("Word Number: "))
Sign up to request clarification or add additional context in comments.

Comments

0

Python treats input as a string so you have to convert it to an int to get the number:

wordNumber = int(input("Word Number: "))
addedWords = 0  
wordList = []


while addedWords != wordNumber:
    Word = input("Word: ")
    wordList.append(Word)
    addedWords = addedWords + 1

2 Comments

int is not a cast operator, but a function; you don't need to surround it with (..)
I see.Fixed. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.