r/learnpython • u/ParkingDiligent1002 • Feb 21 '24
HOW TO READ A PYTHON CODE ON REPEAT PLEASE
Hello i am a 14 years old python big noob and i am trying to do my first ever project, i need help for completing iT
import random
value = random.randint(0, 1000)
guess = int(input("What is your first guess ? "))
if value <= guess:
print("Its Less")
if value >= guess:
print("its More")
if value == guess:
print("Correct !!")
so i want to make this guess the number little game but my problem is that every time the guess is not right i have to restart manually the whole program and it looses my guess + changes the value so its basically impossible. Please i want to know how to just read it on repeat without loosing my guess nor the value.
8
u/mopslik Feb 21 '24
While loops are your friend here.
-16
u/ParkingDiligent1002 Feb 21 '24
please just show me how to do it i cant understand and adapt it to my code, only if i see it applied to my code will i understand please
3
u/mopslik Feb 21 '24
I won't write your code for you. It's not a good way to learn.
Read the page linked above. It describes how a
whileloop works -- specifically, it runs as long as a condition isTrue. For example, here is a loop that prints the numbers 1 through 4.num = 1 while num <= 4: print(num) num += 1Run it and see. Walk through the code to see why it does what it does. What does
num = 1do? What doesnum += 1do? What condition must beTruein order for the loop to run, and when does it stop running?Now think about your own code. When would you want the loop to stop in your game? What would be the appropriate condition that keeps the loop running?
You can do this. It might take you a little while (no pun intended) to think about it, but this is easily within your capabilities.
2
u/jose_castro_arnaud Feb 21 '24
From the Python documentation:
https://docs.python.org/3/
https://docs.python.org/3/tutorial/index.htmlWhile loop:
https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programmingIf, for, and other control structures:
https://docs.python.org/3/tutorial/controlflow.html1
u/Everythingcrashing Feb 21 '24
You *can understand it! Break down that each keyword has a specific meaning and proper sequence.
I'm also a noob and i totally hear your frustration.
I don't want to give you the answer because I want you to understand why you need it, which is how you learn to work on code, or really anything.
So here's a hint:
While Loops, refer to allowing a code to execute 'while' the block returns True. So, as long as the code that follows is True, you can keep most of your code here.
Also, if you want to test your code a little easier, bring that range down to like 1-100!
1
u/mopslik Feb 21 '24
if you want to test your code a little easier, bring that range down to like 1-100!
Also, put it in "cheat mode" -- display the random value once you generate it, so you know what the correct answer is. Then you can test above and below it with no issues.
-12
u/PulsatingGypsyDildo Feb 21 '24
1 import random 2 3 4 value = random.randint(0, 1000) 5 6 7 while(True): 8 guess = int(input("What is your first guess ? ")) 9 10 if value < guess: 11 print("Its Less") 12 elif value > guess: 13 print("its More") 14 else: 15 print("Correct !!") 16 break
1
u/Everythingcrashing Feb 21 '24
would highly recommend having some friends to work through coding problems! The python discord is huge and has tons of people in it.
1
1
u/TechGYQ Feb 21 '24
Write it as a loop and def two functions.
One to get a new number and one for the heart of what you're trying to do.
Set a constant called GAME_OVER and set it to False. Have this run until it's true. That will keep the secret number from changing while your program runs.
When you get the secret_number, have it print the number of tries, reset the tries, fetch a new number, change GAME_OVER to False again, and restart the loop.
You need to look up, functions, and while loops, that's what you're missing. Good luck!
2
7
u/woooee Feb 21 '24
if value <= guess:
if value >= guess:
if value == guess:The first two if statements check for an equal condition so it will never get to the third if. See https://bobbyhadz.com/blog/python-for-loop-user-input
7
u/sirgenz Feb 21 '24
As long as the program doesn’t crash (since there’s no logic to stop it before the third conditional), it’ll always get there. It’s not an elif, so the result of the prior if statements have no effect on it
1
1
u/ZebusAquaion Feb 21 '24
wish reddit had a better code editor for old reddit.
You just needed a while True: loop and your good
Keep it up dude!
Should also learn for loops.
Check out this CS50 course from Harvard!
1
1
u/sackofpizza Feb 21 '24
import sys
import random
value = random.randint(0,5)
print(value)
while True:
try:
guess = int(input("guess: "))
if value < guess:
print("go lower")
if value > guess:
print("go higher")
if value == guess:
sys.exit("correct")
except ValueError:
print("oops")
pass1
1
0
Feb 21 '24
[deleted]
0
u/ParkingDiligent1002 Feb 21 '24
ohh ok bro i didnt understand it that way but now i got it thanks a lot
1
12
u/Atypicosaurus Feb 21 '24
What your program does, it thinks of a number, asks you for a single guess, tells whether it's correct, then quits.
If you want the computer to ask more questions, you need to TELL the program to give more chances. One block of code gives you one chance.
Now, for a practice, I would like you to copy the guessing part 3-4 times below the first one, and see what happens. What happens is you will get exactly as many guesses as copies of program. exactly because one block of guess is one chance.
Now how do you tell the program to give you more chance, without copying the same code?
Well, this is why loops are there. One type of loop called for loop, gives you a given number of chance. You can use it if you want, let's say, 5 guesses. Then you tell the computer to execute the guessing code 5 times, and no more chance.
Alternatively, you can tell the code to keep asking until something happens (let's say, until the user figured it out), that's what the while loops do.