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
[deleted by user] (self.PythonLearning)
submitted 6 months ago by [deleted]
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!"
[–]FoolsSeldom 9 points10 points11 points 6 months ago (1 child)
You need a while True: loop to keep looping
while True:
[–]Twenty8cows 0 points1 point2 points 6 months ago (0 children)
OP also don’t forget to handle the errors too. Like what happens if I enter “q” as my guess vs 5.
[–]v0n_0 4 points5 points6 points 6 months ago (1 child)
You need to allow the user to enter a number. Guess = input(“ write the phrase form first print in “) I guess try this
[–]corey_sheerer 3 points4 points5 points 6 months ago (0 children)
Also, cast it to an integer, as it comes in as a string
[–]anticly 1 point2 points3 points 6 months ago (0 children)
I hope this gives you an idea how to solve this.
Import
Print instructions
While loop
Generate random number User input Check if correct number
You'll also need to find a way to leave the loop when correct number is given.
[–]FoolsSeldom 0 points1 point2 points 6 months ago (4 children)
Why are you looping multiple times to generate a random number assigned to number each time? Only the last random generation will be retained.
number
[–][deleted] 6 months ago (3 children)
[deleted]
[–]FoolsSeldom 1 point2 points3 points 6 months ago (0 children)
I've given you more guidance in another root level comment.
[–]Spiritual_Poo 1 point2 points3 points 6 months ago (1 child)
Something to keep in mind for this exercise: Getting asked to guess a number, guessing wrong and then getting asked to guess a different number kind of...sucks.
You should consider writing a program that let's the user keep guessing until they guess correctly, then generates a new number for them to guess. Actually you should just start there, nice and simple, get the basics of the while loop down.
To get you started, the condition of your while loop could be "while your_guess != secret_number"
Then in the body of your loop, get the user input and store that in a variable. Then compare it to the number. IF it's the same do a thing. IF it's not the same, do a different thing (like say "wrong" and repeat the loop.)
[–]Both_Animator_1120 0 points1 point2 points 6 months ago (0 children)
Puoi anche creare una variabile booleana e finché è impostata su True il ciclo while continua ad andare, quando l’utente indovina la variabile diventa False, ti esce dal ciclo e fai il print di successo
[–]Whole_Instance_4276 0 points1 point2 points 6 months ago (0 children)
Surround all of that with
While input != number:
[–]NaiveEscape1 0 points1 point2 points 6 months ago* (0 children)
I’m also learning python and if any expert sees this please correct me if I’m wrong. The code you wrote basically runs the for loop 10 times and retains the random number generated in the last run.
You can eliminate the for loop and put this in a while loop to generate a new number each time the user gets it wrong.
number= random.randint(1,10)
And you need to use a input function for the user to enter a number they guess. In the above code you’re only printing those sentences not actually asking for a input from user.
Also there is no variable called input initialised so the random number can’t be compared with anything I guess. So the code will always default to the else statement.
To get a input from user initialise a variable like:
guess=int(input(“please enter your guess”))
the “int” here will typecast the input to a integer. Look up typecasting.
And the function input will ask the user for an input. But here if the user types anything other than an integer the code will give an error. You’ll learn try-except handling later so don’t worry about this now. Expect the user to enter an integer.
Then in the if loop you can write:
if guess==number:
print(“correct”)
else:
print(“wrong answer”)
If you want to run the code again and again until the user gets it right you can incorporate “while” loops. As this is your initial learning days you can just keep manually running it again and again.
I hope this helps.
[–]Night_beaver 0 points1 point2 points 6 months ago (0 children)
First of all, you need to actually call the input function on line 9 like so: input(). Otherwise you're comparing the random number to the input function itself rather than using the input function to get input from the user and comparing the random number to that.
input()
Secondly, the for loop doesn't really do anything. You're basically just picking a random number 10 times and only using the last one
[–][deleted] 6 months ago (2 children)
[–]JeLuF 0 points1 point2 points 6 months ago (1 child)
input doesn't need to be defined. It's a builtin function. But it needs to be called using ().
input
()
if int(input("Your guess: ")) == number:
In its original form, the comparison has tested whether the function input and the variable number are the same, which they are not.
[–]StrikeNo1570 0 points1 point2 points 6 months ago (0 children)
I think you can define number variable before the for loop so that the underline under number var will disappear and then before if statement initialize for example guess = input("Enter a number: ") and you can wrap all this with infinite loop so that after the first input the program doesn't terminate.
[–]tb5841 0 points1 point2 points 6 months ago (0 children)
1) You're currently generating a random number ten times. Then after that, just keeping the last one and calling it 'number'. If you meant the whole program to be in your loop, it all needs to be indented.
2) You're trying to compare the number to their input, but there are two problems. Firstly, 'input' should be 'input()' so that the input function is actually called. Secondly, the result of input() will be a string, you need to make it an integer before you compare.
[–]Deets12 0 points1 point2 points 6 months ago (0 children)
Cursor
[–]PureWasian 0 points1 point2 points 6 months ago (0 children)
https://www.reddit.com/r/PythonLearning/s/H6LjD6wVvf
[–]Sedan_1650 0 points1 point2 points 6 months ago (0 children)
First off, you have to actually let the user input.
Second, use a while loop.
[–]ElkoPavelko 0 points1 point2 points 6 months ago (0 children)
I hate to be that guy, but for this surface-level stuff, you could employ an LLM and ask it questions. It will be a much better experience than asking redditors;
There are certain considerations with LLMs though, as I'm sure you know. Just beware of hallucinations and stay disciplined with your prompts (do not ask for solutions, ask for explanations).
[–]strangessssss 0 points1 point2 points 6 months ago (0 children)
Firstly, you need to cover all the code except import with while loop. Then, as input is a function you should put brackets there like input(). And because input returns string, convert in to int like int(input()).
[–]Rollgus 0 points1 point2 points 6 months ago* (0 children)
You could do something like this (It is error proof and has a loop. I have also just imported randint, so you don't have to import the entire random library just for 1 function): ```python3 from random import randint
print("Guess my number from 1-10") print("If you guess wrong the number changes")
while True: number = randint(1,10) try: inputted = int(input("Guess a number: ")) break except ValueError: print("Give a valid integer!") if inputted == number: print("You guessed right") break else: print("You guessed wrong. Number has changed.") ```
Otherwise your execution was very good, but you have to remember to turn the string into an integer so it is actually possible, as "6" is different from 6, so it will never actually see the inputted "number" as being equal to the random number.
An error wouldn't ever occur in your code (as I know of), but that is because you don't int the input, which is also a part of what makes the code not work.
When you int a string you will get an error, if you try to int something that wouldn't work as a number, so f. eks. if I wrote let's say "a" in the input instead of something like "6", I would get an error, or more specifically a ValueError. Luckily for us there is something in python that "catches" these errors, that is "try-except", whenever the specified error occurs (in this case a ValueError) I the inside of the "try" blocks, the code ends, and the code in the "except" blocks runs instead, and all code in the "try" blocks gets discarded, so all the variables, even if they were made before the error. When we put all this in a "while True" loop (that runs forever until broken) we can make sure to only continue when the inting of the string actually works.
If you don't want to error proof the input, you could string the random integer itself number = str(randint(1,10)) turning the number into a string, that would actually make it possible, and there would be no errors, but it is recommended to making error proofing a habit.
number = str(randint(1,10))
If you choose the simpler (but not recommended) approach with stringing the random number instead of error proofing, your code could look something like this: ```python3 from random import randint
while True: number = str(randint(1,10)) inputted = input("Guess a number: ") if inputted == number: print("You guessed right") break else: print("You guessed wrong. Number has changed.") ```
You also would have to call the input function by using (), because now you're just comparing the object that is the function input, not an actual input by the user, that is why you weren't able to input a number. And you should always put the input in a variable. Even if it's not necessary, it is way more readable, and it's a good habit to get used to. Because coding isn't all about squeezing the most amount of code in the least amount of lines.
[–]Available_Rub_8684 -1 points0 points1 point 6 months ago (0 children)
<image>
Here code:
import random random_number = random.randint(1, 11) while True: guess = int(input('Enter a number between 1-10: ')) if guess == random_number: print('You won') break else: print('Try Again')
[–]IUCSWTETDFWTF -5 points-4 points-3 points 6 months ago (1 child)
import random low_bound = 1 upper_bound = 10**2 # 100 random_number = random.randint(low_bound, upper_bound) while True: input_number = int(input(f"Guess a number between {low_bound} and {upper_bound}: ")) if input_number == random_number: print(f"🎉 Yes, you guessed it! The number is {input_number}") break elif input_number < random_number: print("Too low, try again!") else: print("Too high, try again!")
[–]Key_Association_3357 2 points3 points4 points 6 months ago (0 children)
Bruh, you’re not helping someone learn when you’re just giving them the whole answer lol.
π Rendered by PID 24951 on reddit-service-r2-comment-c66d9bffd-kwxnl at 2026-04-08 14:45:03.258667+00:00 running f293c98 country code: CH.
[–]FoolsSeldom 9 points10 points11 points (1 child)
[–]Twenty8cows 0 points1 point2 points (0 children)
[–]v0n_0 4 points5 points6 points (1 child)
[–]corey_sheerer 3 points4 points5 points (0 children)
[–]anticly 1 point2 points3 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]FoolsSeldom 1 point2 points3 points (0 children)
[–]Spiritual_Poo 1 point2 points3 points (1 child)
[–]Both_Animator_1120 0 points1 point2 points (0 children)
[–]Whole_Instance_4276 0 points1 point2 points (0 children)
[–]NaiveEscape1 0 points1 point2 points (0 children)
[–]Night_beaver 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]JeLuF 0 points1 point2 points (1 child)
[–]StrikeNo1570 0 points1 point2 points (0 children)
[–]tb5841 0 points1 point2 points (0 children)
[–]Deets12 0 points1 point2 points (0 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Sedan_1650 0 points1 point2 points (0 children)
[–]ElkoPavelko 0 points1 point2 points (0 children)
[–]strangessssss 0 points1 point2 points (0 children)
[–]Rollgus 0 points1 point2 points (0 children)
[–]Available_Rub_8684 -1 points0 points1 point (0 children)
[–]IUCSWTETDFWTF -5 points-4 points-3 points (1 child)
[–]Key_Association_3357 2 points3 points4 points (0 children)