Python Forum
Code won't break While loop or go back to the input? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Code won't break While loop or go back to the input? (/thread-38530.html)



Code won't break While loop or go back to the input? - MrKnd94 - Oct-26-2022

Hello,

I'm trying to input a list with a While loop, and then print the list when it is over 100 (the number), but it keeps going even though I put a break in.

I've been trying and thinking about doing it all day yesterday and today too.

This is my code:

input=int(input('Number? '))

list=[]

while True:
    if input <= 100:
        list.append(input)

    else:
        list.append('over')
        break
print(list)
Any help would be greatly appreciated and try to explain my mistakes too please.

Thanks.


RE: Code won't break While loop or go back to the input? - rob101 - Oct-26-2022

First off; don't use Python key words as names for your variables.

So, use user_input = int(input('Number? ')) or some such.

Also number_list = []

Second; while True: will never fail (as is), so you'll simply keep on appending the number to the number list; I won't say forever, because at some point, something will crash (possibly), but it'll take some time.

If you need any more help, post back.


RE: Code won't break While loop or go back to the input? - Larz60+ - Oct-26-2022

do not name a list 'list', you will overwrite original list and won't be ablt to use it.
same thing with input

Your input statement is outside of the loop, so can't ever change.
mylist = []

while True:
    try:
        number = int(input('Number? '))
    except ValueError:
        print("Please, numeric entry only")
        continue

    if number <= 100:
        mylist.append(number)
    else:
        mylist.append('over')
        break

print(mylist)
test:
Output:
Number? 12 Number? 33 Number? 77 Number? 345 [12, 33, 77, 'over']