Python Forum

Full Version: Code won't break While loop or go back to the input?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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']