r/learnpython • u/anas_mv • 2d ago
CS50 problom 4 Gussing game
this is what im trying to do https://cs50.harvard.edu/python/psets/4/game/
this my code
import random
while True:
try:
level = int(input("Level:"))
if level >= 0:
False
number = random.randint(1, level)
output = number
if output < number:
print("Too small!")
elif output > number:
print("Too large!")
elif output == number:
print("Just right!")
print("Guess:", number)
except ValueError:
passwhen I start and give a number it give ether it stopes it "Just right!" or print both "Just right!" and Guess: so how do I make it don't do that and do what it suppose to do in the problem
0
Upvotes
1
u/lfdfq 2d ago
First, these lines:
What is it trying to do? It looks like it should error if you run it, because of those lines.
There are a few problems I see, but the biggest one is that you never ask the user their guess. You roll a random number AND use that number as the guess. Can you see why that does not work?
1
u/AlexMTBDude 2d ago
First this:
output = numberThen this:
elif output == number:What do YOU think will happen?
1
u/atarivcs 2d ago
This line:
Assigns output to be equal to number.
So of course you will never see the "too small" or "too large" messages...
1
u/Naive_Programmer_232 2d ago edited 2d ago
A couple of things. The assignment says the game should prompt the user for a level and if it is not a 'positive integer' to prompt them again. Your first conditional is wrong.
Part 1: verify level is a positive integer
In addition, the assignment says, if level is a positive integer, get a random number from [1, level] (inclusive), and then ask the user for a guess between 1 and level. Your code does get the random number, however, it doesn't ask the user for the guess.
Part 2: gather guess from user
Then the assignment says when the guess is gathered from the user, you need to validate if the guess itself is also a positive integer.
Part 3: verify guess is a positive integer - structural issue hint
Now this is an interesting piece to the puzzle. Consider where ???? is, what happens if you put
continuethere?Answer: control will go back to the top of the loop, just like it would with the level verification part. However, in the guess case, this is an issue. If control goes back to the top of the loop, then the user will be asked for the level again!
So, this is starting to point at a structural issue. Maybe the level verification part should be in its own loop, while the guess verification part should be in a separate loop. Maybe the two loops could be on the same level or could be nested within each other?
Let's consider two separate loops on the same level. One loop for level validation and the other loop for guess validation. In addition let's separate the int() failure ValueError from the level error when it's not a positive integer; and also from the guess error when it's not a positive integer or it's greater than level.
To do this, we'll create two custom exception classes, InvalidLevelError and InvalidGuessError, and use two except statements in each loop's try/except/else chain to distinguish between the ValueError caused by int() breaking and the appropriate custom exception ('exceptions' are python's term for errors).
Part 4: new structure to verify level and guess with custom error classes + try/except/else
The above is for sake of example. By no means is this the only way to go about this nor is the best way of doing it. I'm going to leave Part 4 the way it is so you can figure out the rest.
Ideally, what should happen is the TO-DO comment portion should be edited and guess should be compared with rand_num.
If
guess!=rand_num, then the loop should do what? And what should happen first per the case (larger / smaller)? When should thebreakapply?? I'll leave it for you to decide.Once you get your program working. Here's some extra challenges to consider implementing:
There's a lot you can do with a project like this. This is a great starting point.
Anyway, hopefully this clarifies things a bit more. Best of luck with everything!