Issue
The program is such that the user selects a number in his mind and does not input it into the computer. (Number in range(1,99))
The program guesses and prints a number. randint(1,99) The printed number creates three modes: 1- is it larger than the number you have in mind when you type the letter ’s' into the program saying that the number in your mind is smaller than the number printed and the program has to guess and display another number (It should be noted that in this case, by typing ’s’ in the program, one has to guess a smaller number than his previous guess to finish the program sooner.)
2-or the printed number is smaller than the number you have in mind when you type the letter ‘l' into the program saying that the number in your mind is larger than the number printed and the program has to guess and display another number (It should be noted that in this case, by typing 'l', the program must guess a larger number than its predecessor in order to finish the program sooner.)
3=or is the printed number the same number that you had in mind and by typing the letter 'd' into the program you guess right and the program ends.
i try this
from random import randint
guess=randint(1,99)
print(guess)
my_answer=''
while my_answer != 'd':
my_answer=input()
if my_answer=="l":
n=randint(guess,99)
print(n)
elif my_aswer=='s':
m=randint(1,guess)
print(m)
else:
print('done')
But I don't know how that doesn't guess from the previous numbers again and make the range guess smaller.
Solution
You need to change guess
instead of using new variable n
. You can also set the range of the guesses every time
low = 1
high = 99
guess = randint(low, high)
print(guess)
my_answer = ''
while my_answer != 'd':
my_answer = input()
if my_answer == "l":
low = guess
guess = randint(guess, high)
print(guess)
elif my_answer == 's':
high = guess
guess = randint(low, guess)
print(guess)
else:
print('done')
Example output
20
l
81
s
63
s
22
l
47
l
60
s
48
l
59
s
57
s
56
s
53
d
done
Answered By - Guy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.