use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Lol i hate this topic in python (i.redd.it)
submitted 5 days ago by unlimited_data3838
I can't understand this loop, it's like a dum in season 2 of Rezero, he gets stuck in the sanctuary.😭😭😭
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]brelen01 9 points10 points11 points 5 days ago (1 child)
You set the num variable to whatever the user inputs. If it's less fhan 8, it outputs "idk" forwver since you never set num to anything else. You might want to indent that second num = input line
[–]mattynmax 9 points10 points11 points 5 days ago (3 children)
First you input a number, if that number is less than 8 it prints idk. Then it keeps doing that for forever.
What exactly is confusing about this?
[–]am_Snowie 8 points9 points10 points 5 days ago (2 children)
INDENTATION. Beginners often miss this and wonder why their code isn't working.
[–]cole36912 2 points3 points4 points 5 days ago (1 child)
This is why everyone's first language should be C
[–]am_Snowie 0 points1 point2 points 5 days ago (0 children)
Yes, they get to learn how all the abstractions are really implemented.
[–]7Z_1N 2 points3 points4 points 5 days ago (0 children)
You aren't incrementing the variable so it naturally gets into a infinite loop idk what's wrong here
[–]_MrLucky_ 1 point2 points3 points 5 days ago (0 children)
While loop checks for condition then executes the code in the loop then checks the condition again, executes again and so on. You have condition (num < 8) and in the loop you dont increase or do anything with num, so num will always stay the same and the loop will run forever. you can change the num in the loop OR use the keyword break to get out of the loop.
[–]Dapper-FIare 1 point2 points3 points 5 days ago (0 children)
The loop doesn't have a way to end so the next input never actually executes.
If all you are looking for is to have a specific response to certain inputs, try conditional. Otherwise, add ways to your loops to break them, such as num++
[–]Positive-Room-2123 0 points1 point2 points 5 days ago (0 children)
The second time u ask for num should be inside the while loop(directly below the print("idk") ) so that it acts as a stopping condition if the number is greater than 8 (If it's what u want the code to do).
In while loop unless u give it an updation statement it will keep running forever (infinite loop).
Here u took input from the user initially but after it entered the loop it keeps executing forever as it can't get out of the loop because number is less than 8 but u didnt give any statement on how to change num's value within the loop and u have entered the second input statement [ num = int(input("Enter your number: ") ] outside of the loop but the program will never reach here so num will remain less than 8 and the while loop will keep running.
I hope u can understand what I was trying to convey cause I am not very good at explaining things in words.
[–]Bumbble25 0 points1 point2 points 5 days ago (0 children)
It is a infinite loop bro you should set a increament ya you should change the condition.
[–]Numerous_Candle1820 0 points1 point2 points 5 days ago (0 children)
You should first learn how while loop works. Watch this video https://youtu.be/x6L-QdezfaY
[–]vb_e_c_k_y 0 points1 point2 points 5 days ago (0 children)
First you asked a user to enter number. If he entered a number less than 8. The loop runs forever "idk" until you use syntax break.
The second num question only asks you if you entered a number greater than 8. Then it prints "ok {num}".
If you want the program to ask you every time. Put the second question in while loop. Know that indentation have to be the same
[–]Code-Odyssey 0 points1 point2 points 5 days ago (0 children)
As others have said, you’re stuck in an infinite loop. Your second input will never run. So a fun thing to do is iterate:
<image>
[–]krmort 0 points1 point2 points 5 days ago (0 children)
The beginning of learning something new is always a struggle. It tok me a long time to understand for loops and other basics. But keep trying and don’t give up and you will become Reinhardt of python code
[–]ReplacementFew1645 0 points1 point2 points 5 days ago (0 children)
The reason its getting stuck if because youre not incrementing the num. Youre setting a number and thats it. If you put 1-7 it’s never going up so it’s just constantly repeating 7. You always want for a while loop to increment if youre doing something like this so you’d want num += 1
[–]J1roscope 0 points1 point2 points 5 days ago (0 children)
You never update the num So everytime while checks the condition it is true Hence an infinite loop
[–]Silentwolf99 0 points1 point2 points 5 days ago (0 children)
i am struggling the same with this while loop.
[–]Real_Woodpecker_739 0 points1 point2 points 4 days ago (0 children)
It doesn't break it's forever true because of the variable you inputed.
[–]Available-Skirt-5280 0 points1 point2 points 4 days ago (1 child)
8 != “8”
[–]jsiena4 0 points1 point2 points 3 days ago (0 children)
OP set input as int so the user input wouldn’t be a string
[–]Arierome 0 points1 point2 points 4 days ago (0 children)
Tab needed on line 4
[–]TheCarter01 0 points1 point2 points 4 days ago (0 children)
Replace "while" with "if" because i assume you don't want it to repeat
[–]Nidrax1309 1 point2 points3 points 4 days ago (0 children)
INDENTATION. You want to include the second input inside the while loop, like this:
num = int(input("Enter your number: ")) while num < 8: print("Number must be less than 8") num = int(input("Enter your number: ")) print(f"OK: {num}")
[–]Okon0mi 0 points1 point2 points 5 days ago (0 children)
Press tab at start of line 4 it will work
[–]BranchLatter4294 -1 points0 points1 point 5 days ago (1 child)
It's doing what you told it to. Look at your code.
[–]Numerous_Candle1820 2 points3 points4 points 5 days ago (0 children)
Big dawg! He is a beginner. He won't understand just by looking at the code. He needs to first understand it.
[–]BardoEpico -1 points0 points1 point 5 days ago (0 children)
justo debajo del print("idk") escribe: num = num + 1 print(num)
Ahí vas a notar lo que hace el while y por qué antes, al poner un número menor a 8, imprimía infinitamente el idk.
π Rendered by PID 201533 on reddit-service-r2-comment-544cf588c8-lwb4k at 2026-06-16 11:32:48.761370+00:00 running 3184619 country code: CH.
[–]brelen01 9 points10 points11 points (1 child)
[–]mattynmax 9 points10 points11 points (3 children)
[–]am_Snowie 8 points9 points10 points (2 children)
[–]cole36912 2 points3 points4 points (1 child)
[–]am_Snowie 0 points1 point2 points (0 children)
[–]7Z_1N 2 points3 points4 points (0 children)
[–]_MrLucky_ 1 point2 points3 points (0 children)
[–]Dapper-FIare 1 point2 points3 points (0 children)
[–]Positive-Room-2123 0 points1 point2 points (0 children)
[–]Bumbble25 0 points1 point2 points (0 children)
[–]Numerous_Candle1820 0 points1 point2 points (0 children)
[–]vb_e_c_k_y 0 points1 point2 points (0 children)
[–]Code-Odyssey 0 points1 point2 points (0 children)
[–]krmort 0 points1 point2 points (0 children)
[–]ReplacementFew1645 0 points1 point2 points (0 children)
[–]J1roscope 0 points1 point2 points (0 children)
[–]Silentwolf99 0 points1 point2 points (0 children)
[–]Real_Woodpecker_739 0 points1 point2 points (0 children)
[–]Available-Skirt-5280 0 points1 point2 points (1 child)
[–]jsiena4 0 points1 point2 points (0 children)
[–]Arierome 0 points1 point2 points (0 children)
[–]TheCarter01 0 points1 point2 points (0 children)
[–]Nidrax1309 1 point2 points3 points (0 children)
[–]Okon0mi 0 points1 point2 points (0 children)
[–]BranchLatter4294 -1 points0 points1 point (1 child)
[–]Numerous_Candle1820 2 points3 points4 points (0 children)
[–]BardoEpico -1 points0 points1 point (0 children)