I'm learning how to program in Python and I found 2 tasks that should be pretty simple, but the second one is very hard for me.
Basically, I need to make a program where computer guesses my number. So I enter a number and then the computer tries to guess it. Everytime it picks a number I need to enter Lower
or
Higher
. I don't know how to do this. Could anyone advise me on how to do it?
For example [number is 5]:
computer asks 10?
I write Lower
computer asks 4?
I write Higher
Program: I already made a program which automatically says Higher
or Lower
but I want to input Lower
or Higher
as a user.
from random import randit
number = int[input["Number? "]]
attempts = 0
guess = 0
min = 0
max = 100
while guess != number:
guess = randint[min,max]
print[guess,"?"]
if guess < number:
print["Higher"]
min = guess
elif guess > number:
print["Lower"]
max = guess
attemps += 1
print["I needed", attempts, "attemps"]
dedObed
1,24310 silver badges19 bronze badges
asked Oct 18, 2016 at 17:49
4
You may want to put in a case for if it doesn't match you input, also I think you need a case for when the guess finally equals the number, you may want to allow an input for "Found it!" or something like that.
from random import randint
number = int[input["Number? "]]
attempts = 0
guess = 0
min = 0
max = 100
while guess != number:
guess = randint[min,max]
print[guess,"?"]
answer = str[input["Higher/Lower? "]]
if answer == 'Higher':
min = guess
elif answer == 'Lower':
max = guess
attempts += 1
print["I needed", attempts, "attempts"]
answered Oct 18, 2016 at 18:13
HoopdadyHoopdady
2,2683 gold badges25 silver badges39 bronze badges
1
from random import randit
attempts = 0
guess = 0
min = 0
max = 100
while guess != number:
number = int[input["Number? "]]
guess = randint[min,max]
print[guess,"?"]
if guess < number:
print["Higher"]
min = guess
elif guess > number:
print["Lower"]
max = guess
attemps += 1
print["I needed", attempts, "attemps"]
problem is your using a loop but inputting value once at start of app . just bring input inside the while statement hope this help
answered Oct 18, 2016 at 18:32
1
from random import randint
print['choos a number in your brain and if guess is true enter y else any key choose time of guess: ']
print["define the range [A,B] :"]
A = int[input["A: "]]
B = int[input["B: "]]
time = int[input["time:"]]
while time != 0:
ran = randint[A, B]
inp = input[f"is this {ran} ?"]
time -= 1
if inp == "y":
print["bla bla bla computer wins!"]
break
print["NOPE!"]
if time == 0:
print["computer game over!"]
break
answered Dec 24, 2018 at 5:54
mujadmujad
5446 silver badges13 bronze badges
from random import *
number = int[input["Number? "]]
attempts = 0
guess = 0
min = 0
max = 100
attemps =0
guess = randint[min,max]
while guess != number:
userInput=input[str[guess]+"?"]
if userInput.lower[]=="lower":
max=guess
elif userInput.lower[]=="higher":
min=guess
attemps += 1
guess = randint[min,max]
print["I needed", attemps, "attempts to guess ur number ="+str[guess]]
output:
Number? 5
66?lower
63?lower
24?lower
19?lower
18?lower
10?lower
4?higher
9?lower
6?lower
4?higher
I needed 10 attempts to guess ur number =5
answered Dec 24, 2018 at 6:19