英文:
Python guessing game exercise - confused about the description and the math
问题
#PC should guess in no more than %s tries\n" % max_tries
英文:
My questions are on the bottom.
This exercise is based on a guessing game where the user (you) inputs a lower and upper bound int and then the PC picks a random number within those boundaries. Your job is to guess the number and the program tells the user if the guess is greater than, less than, or equal to the number the PC has chosen. It's most strategic to make guesses from the middle. The number of tries is recorded.
Here is the code:
import random
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
myNumber = random.randint(smaller, larger)
count = 0
while True:
count += 1
userNumber = int(input("Enter your guess: "))
if userNumber < myNumber:
print("Too small!")
elif userNumber > myNumber:
print("Too large!")
else:
print("Congratulations! You've got it in", count, "tries!")
break
Here is a sample output:
#Output example
Enter the smaller number: 1
Enter the larger number: 100
Enter your guess: 50
Too small!
Enter your guess: 75
Too large!
Enter your guess: 63
Too small!
Enter your guess: 69
Too large!
Enter your guess: 66
Too large
Enter your guess: 65
You've got it in 6 tries!
Now here is the exercise description:
"Modify the guessing-game program so that the user thinks of a number that the computer must guess.
The computer must make no more than the minimum number of guesses, and it must prevent the user from cheating by entering misleading hints.
Use I'm out of guesses, and you cheated
and Hooray, I've got it in X tries
as your final output.
(Hint: Use the math.log
function to compute the minimum number of guesses needed after the lower and upper bounds are entered.)"
Here are two sample outputs I should be able to reproduce:
Enter the smaller number: 0
Enter the larger number: 10
0 10
Your number is 5
Enter =, <, or >: <
0 4
Your number is 2
Enter =, <, or >: >
3 4
Your number is 3
Enter =, <, or >: =
Hooray, I've got it in 3 tries!
and
Enter the smaller number: 0
Enter the larger number: 50
0 50
Your number is 25
Enter =, <, or >: <
0 24
Your number is 12
Enter =, <, or >: <
0 11
Your number is 5
Enter =, <, or >: <
0 4
Your number is 2
Enter =, <, or >: <
0 1
Your number is 0
Enter =, <, or >: >
1 1
Your number is 1
Enter =, <, or >: >
I'm out of guesses, and you cheated!
Now here is my code:
from math import log2
#get the lower and upper bounds that the PC can guess between
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
#this should calculate the maximum number of tries the PC can do
max_tries = round(log2(larger-smaller+1))
print("PC should guess in no more than %s tries\n" % max_tries)
count = 0
while count <= max_tries:
count += 1
pc_guess = (larger+smaller) // 2 #This is the PC's guess
print("%d %d" % (smaller, larger)) #boundry
print("Your number is ", pc_guess) #printing PC's guess
op = input("Enter =, <, or >: ") #giving PC more info
if op == '<': #the number is less than the PC's guess
larger = pc_guess - 1
elif op == '>': #the number is greater than the PC's guess
smaller = pc_guess + 1
elif op == '=':
print("Hooray I've got it in", count, "tries")#PC guessing correct
break
if count > max_tries:
print("I'm out of guesses, and you cheated")
Am I even thinking about this exercise correctly?
Furthermore, I don't understand how to get the PC to get the right answer in "no more than the minimum number of guesses," which I calculated as log2(larger-smaller+1)
in my code. Calculating the average of the upper and lower bound still results in greater tries than the log2 result.
Also, am I even calculating the PC's best guess correctly (the average calculation)?
答案1
得分: 1
以下是翻译好的部分:
我认为,如果你将结果四舍五入,可以帮助你很多,以获得一个更多可能的提示。像这样:
import math
.
.
.
max_tries = math.ceil(math.log(larger-smaller))
你也可以在这篇帖子中查看更多详细信息:https://stackoverflow.com/questions/60068321/guessing-game-in-python-wont-print-elseif
英文:
I think that it would help you a lot if you rounded up the result, so as to get one more possible hint. Like this:
import math
.
.
.
max_tries = math.ceil(math.log(larger-smaller))
You can see more details on this post, too: https://stackoverflow.com/questions/60068321/guessing-game-in-python-wont-print-elseif
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论