Reputation: 19
I'm trying to write a simple code that takes a user input and appends whatever number user typed in into a list. Lastly when the user types "done", the loop stops.
a = []
user_input = input("Please enter a number: ")
while user_input:
if user_input == "done":
break
int(user_input)
a.append(user_input)
print(a)
But for some reason when I type done, the loop doesn't stop and the list does not get printed.
Upvotes: 0
Views: 1740
Reputation: 53
The user_input
never changes inside the loop, so it will never terminate. You need to put the input
call inside the loop.
If the list still doesn't get printed (as you seemed to indicate on another answer), the problem is somewhere else. It could possibly be a problem with the interpreter/terminal, since from your question it seems you are able to input more data even though the loop should prevent you from doing that.
Upvotes: 0
Reputation: 2301
Put the input()
inside the loop like so:
a = []
while True:
user_input = input("Please enter a number: ")
if user_input == "done":
break
user_input = int(user_input)
a.append(user_input)
print(a)
Upvotes: 1
Reputation: 65
I would suggest checking the indentation then and make sure that everything is indented correctly. I just copied your code here : https://www.programiz.com/python-programming/online-compiler/ and it printed out the list . The print statement needs to have the same “level of indentation” as the “while true “ statement
Upvotes: 0