我是Python的新手,我正在制作一个程序,在该程序中,计算机会在一定范围内生成一个随机数,然后尝试猜测它所选择的数字。我通过使用for循环让计算机猜出多个数字(在这种情况下为2)。我还跟踪每次循环迭代后的猜测次数。在那之后,我将在循环结束后取平均值。但是,当我试图获得平均值时,它不会打印。我不知道代码有什么问题。有人可以帮我解决这个问题吗?
import random
def guess():
the_num = random.randint(1, 100)
print('The number to guess is',the_num)
comp_guess = random.randint(1, 100)
print('The computer guesses ', comp_guess)
tries = 1
while comp_guess != the_num:
print('The computer guesses ', comp_guess)
tries += 1
if comp_guess == the_num:
break
else:
comp_guess = random.randint(1, 100)
for i in range(2):
guess()
print('The computer took',tries,'guesses')
print('The computer guessed it right!')
print('The computer guessed',(tries/2),'times on average')
# Trying to calculate the average number of guesses after the loop is finished
答案 0 :(得分:0)
通过在内部调用guess,您正在创建一个没有基本情况的递归函数,因此它永远不会结束。更好的方法是使循环出现在实际代码中。在下面的代码中,我还可以自由地使函数灵活,允许根据输入调用它。
import random
def guess(num_loops):
total_tries = 0
for i in range(num_loops):
the_num = random.randint(1, 100)
print('The number to guess is',the_num)
comp_guess = random.randint(1, 100)
print('The computer guesses ', comp_guess)
tries = 1
while comp_guess != the_num:
# print('The computer guesses ', comp_guess)
tries += 1
print(tries)
if comp_guess == the_num:
break
else:
comp_guess = random.randint(1, 100)
total_tries += tries
print('The computer took',tries,'guesses')
print('The computer guessed it right!')
print('The computer guessed',(total_tries/num_loops),'times on average')
# Trying to calculate the average number of guesses after the loop is finished
guess(2)
答案 1 :(得分:0)
你所拥有的是递归,即函数调用自身时。
在你的情况下,你不需要递归,只需要一个函数和代码来调用它。
你的功能应该做一件事,做得好。在这种情况下,您的功能是让计算机猜测它生成的数字。
def guess():
the_num = random.randint(1, 100)
print('The number to guess is',the_num)
comp_guess = random.randint(1, 100)
print('The computer guesses ', comp_guess)
tries = 1
while comp_guess != the_num:
print('The computer guesses ', comp_guess)
tries += 1
if comp_guess == the_num:
break
else:
comp_guess = random.randint(1, 100)
现在看来你想要这个代码运行两次。因此,计算机会猜出多个数字,并且对于它试图猜测的每个数字都有很多尝试。
在这种情况下,您只需要在函数外多次调用该函数。
for i in range(2):
guess()
此代码不是缩进的,也不是函数的一部分。
要了解有关功能的更多信息,请访问Python文档:https://docs.python.org/3/tutorial/controlflow.html#defining-functions
我还会链接到我强烈推荐的一本书:http://inventwithpython.com/chapter6.html
这些资源将帮助您了解有关功能的更多信息。
希望这个答案对你有所帮助!如果您有任何其他问题,请在下面发表评论!
答案 2 :(得分:0)
您不需要或希望guess()
功能自行调用,因为它当前编码的方式永远不会返回。
以下是我将如何重新组织您的代码以避免这样做:
import random
NUM_GUESSES = 2
def guess():
the_num = random.randint(1, 100)
print('The number to guess is', the_num)
tries = 0
comp_guess = None
while comp_guess != the_num:
comp_guess = random.randint(1, 100)
print('The computer guesses', comp_guess)
tries += 1
return tries
total_tries = 0
for i in range(NUM_GUESSES):
tries = guess()
total_tries += tries
print('The computer took', tries, 'guesses')
print('The computer guessed it right!')
average = total_tries / NUM_GUESSES
print('The computer guessed', average, 'times on average')
示例输出:
The number to guess is 88
The computer guesses 58
The computer guesses 19
The computer guesses 55
The computer guesses 16
The computer guesses 85
The computer guesses 24
The computer guesses 76
The computer guesses 36
The computer guesses 18
The computer guesses 58
The computer guesses 27
The computer guesses 1
The computer guesses 2
The computer guesses 100
The computer guesses 74
The computer guesses 49
The computer guesses 26
The computer guesses 76
The computer guesses 28
The computer guesses 38
The computer guesses 27
The computer guesses 100
The computer guesses 7
The computer guesses 50
The computer guesses 54
The computer guesses 80
The computer guesses 36
The computer guesses 39
The computer guesses 61
The computer guesses 28
The computer guesses 11
The computer guesses 38
The computer guesses 27
The computer guesses 29
The computer guesses 70
The computer guesses 45
The computer guesses 18
The computer guesses 65
The computer guesses 40
The computer guesses 88
The computer took 40 guesses
The computer guessed it right!
The number to guess is 99
The computer guesses 73
The computer guesses 99
The computer took 2 guesses
The computer guessed it right!
The computer guessed 21.0 times on average