Python Forum
Code won't break While loop or go back to the input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code won't break While loop or go back to the input?
#1
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.
Reply
#2
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,074 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,056 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  How to break out of a for loop on button press? philipbergwerf 6 1,786 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  Help Switching between keyboard/Mic input in my code Extra 1 1,100 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  break out of for loop? User3000 3 1,465 May-17-2022, 10:18 AM
Last Post: User3000
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,496 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,706 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  "while" loop is looping back too early mangurian 1 1,285 Jan-28-2022, 09:15 AM
Last Post: ibreeden
Exclamation question about input, while loop, then print jamie_01 5 2,686 Sep-30-2021, 12:46 PM
Last Post: Underscore
  tkinter control break a while loop samtal 0 2,407 Apr-29-2021, 08:26 AM
Last Post: samtal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020