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
  Using a For Loop to subtract numbers from an input function. Anunderling 9 2,659 Sep-22-2025, 08:56 PM
Last Post: deanhystad
  data input while debugging causes unwanted code completion fred1232 2 2,590 Sep-14-2025, 03:32 PM
Last Post: deanhystad
  Break within an if statement inside a while loop not working Python_more_on 2 1,839 Sep-03-2025, 12:55 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 2,995 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to convert while loop to for loop in my code? tatahuft 4 1,619 Dec-21-2024, 07:59 AM
Last Post: snippsat
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 3,600 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 2,827 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  How to break out of a for loop on button press? philipbergwerf 6 4,642 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  Help Switching between keyboard/Mic input in my code Extra 1 2,420 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  break out of for loop? User3000 3 3,217 May-17-2022, 10:18 AM
Last Post: User3000

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.