Reputation: 55
So I'm trying to write a code where the computer guesses the number the user thinks of. I used a range between (x,y) and gave x and y the values of 0 and 10.
The computer chooses a random number between these and than asks if than number is correct, if it isn't than it asks if it's higher or lower.
Here comes the tricky part, or actually just the way I wanted to solve it. If the user says it's a higher number, then it sets x
to be the guess number, and loops back to the guessing part, and goes again, if it's lower, y
is set to be the guess.
This is the code I've written for that:
import random
x = 0
y = 10
guessing_loop = "y"
no_guess = 0
while guessing_loop == "y":
guess = random.randint(x - 1,y + 1)
no_guess = no_guess + 1
print("The number I think you thought of is",guess,".")
TF = input("Did you think of that number?")
if TF in {"yes","y","yeah"}:
print("Hell yeah, the number of guesses I had was:",no_guess,".")
elif TF in{"no","nah","nay","n","nope"}:
HL = input("Then was it a higher or a lower number?")
if HL in {"higher","h"}:
x = guess
elif HL in {"lower","l"}:
y = guess
input()
Why doesn't this code work?
This code simply does not do what I expect it to do, it doesn't look for a random number between the guesses or I'm not even sure what is wrong.
Upvotes: 0
Views: 187
Reputation: 27283
Several issues:
lower_bound
and upper_bound
instead of x
and y
.random.randint(x - 1,y + 1)
, you want random.randint(x + 1,y - 1)
Upvotes: 3