I was working with the Walrus operator. I was getting the user to guess a number between 0 and 10. Their guess is then added to a list full of their guesses. But once they enter a 0, it just prints out the list.
The problem I am having is that the first guess doesn't get put in the list. I tried printing out the list every time a value was added, but the list just won't include the first input. How can I get the Walrus operator to append the first value to my list?
Here's my code:
import traceback
def checkAnswerWalrus():
listOfGuesses = []
while (numberGuessed := int(input(f"Guess a number between 0 to 10 : "))) != 0:
listOfGuesses.append(numberGuessed)
print(listOfGuesses)
if numberGuessed == 0:
print(listOfGuesses)
return(True)
if __name__ == "__main__":
try:
numberGuessed = int(input(f"Guess a number between 0 to 10 : "))
checkAnswerWalrus()
except Exception as error:
traceback.print_exc()
finally:
print(f":)")
Here's the example output, where the "2" isn't being appended to the list :
C:/Python3.8/python.exe c:/Users/mayab/Desktop/ict/python/misc/testWalrus.py
Guess a number between 0 to 10 : 2
Guess a number between 0 to 10 : 3
[3]
Guess a number between 0 to 10 : 4
[3, 4]
Guess a number between 0 to 10 : 5
[3, 4, 5]
Guess a number between 0 to 10 : 6
[3, 4, 5, 6]
Guess a number between 0 to 10 : 7
[3, 4, 5, 6, 7]
Guess a number between 0 to 10 : 0
[3, 4, 5, 6, 7]
:)
The first question is asked and the first number input in the
try
section, where you do not append the value to a list. Just remove it and letcheckAnswerWalrus()
do the whole work.