【问题标题】:Python guessing game, counting number of guessesPython猜谜游戏,计算猜测次数
【发布时间】:2015-04-01 18:05:48
【问题描述】:

我的计算机科学简介老师给了我们一个骰子猜谜游戏挑战,虽然我已经设法弄清楚了大部分组件,但最后一部分是返回正确的数字是什么(一旦他们猜到了)以及他们试图弄清楚的次数。我的代码返回了所有这些,但它并没有说明猜测的人是否两次猜测相同的数字。有没有办法告诉他们在不考虑任何重复数字的情况下,他们需要多少次猜测才能找到一个数字?

这是我目前的代码:

import random

give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")
guess_number = 1

dice = random.randint(1,6)
while give_number != dice:
    if give_number > dice:
        give_number = input("Sorry, that answer is too high! Try again!\n ")
        guess_number = guess_number +1
    if give_number < dice:
        give_number = input("Sorry, that answer is too low! Try again!\n ")
        guess_number = guess_number +1

print "Congratulations, you were right, the answer was {}! It took you {} tries.".format(dice, guess_number)

【问题讨论】:

  • 修正缩进。 while 下的 if 语句需要移动到我的一个标签空间

标签: python count dice


【解决方案1】:

要检测重复的猜测,我们必须跟踪所有之前的猜测。

我们只需要知道某个特定数字之前是否被猜到过 - 而不是猜到了多少次。

这是使用set 的理想场所(请参阅Python docs)。

从一个空集开始(而不是从guess_number = 1 开始),然后将每个猜测添加到这个集。

一旦人们猜对了,您就可以检查集合的大小 - 这是猜测的数量,忽略重复。

【讨论】:

  • 或者用字典看看哪个数字被猜的次数最多
【解决方案2】:

我建议使用列表来追加猜测。我以这种方式更改了您的代码(在我的情况下为 Python 3):

import random

give_number = int(input("We will roll a 6 sided dice. What do you think the number will be?\n "))
guess_number = 1
guessList = []
guessList.append(give_number)

dice = random.randint(1,6)
while give_number != dice:
    if give_number > dice:
        give_number = int(input("Sorry, that answer is too high! Try again!\n "))
        #guess_number = guess_number + 1
        if give_number not in guessList:
            guessList.append(give_number)
    if give_number < dice:
        give_number = int(input("Sorry, that answer is too low! Try again!\n "))
        #guess_number = guess_number + 1
        if give_number not in guessList:
            guessList.append(give_number)

if give_number not in guessList:
    guessList.append(give_number)
print ("Congratulations, you were right, the answer was {}! It took you {} tries.".format(dice, len(guessList)))

如果你想检查输入是整数还是在你的范围内,你可以看看这段代码:

import random

def inputIsInt(inpStr): # checks if the input is an integer in range (1, 7).
    try:
        if int(inpStr) in range(1, 7):
            return True
        else:
            return False
    except ValueError:
        return False

give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")
while not inputIsInt(give_number):
    print("Sorry, your input is not an integer or out of range")
    give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")

guessList = []
guessList.append(int(give_number))
dice = random.randint(1,6)

while int(give_number) != dice:
    if int(give_number) > dice:
        give_number = input("Sorry, that answer is too high! Try again!\n ")
        while not inputIsInt(give_number):
            print("Sorry, your input is not an integer or out of range")
            give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")
        if int(give_number) not in guessList:
            guessList.append(int(give_number))
    if int(give_number) < dice:
        give_number = input("Sorry, that answer is too low! Try again!\n ")
        while not inputIsInt(give_number):
            print("Sorry, your input is not an integer or out of range")
            give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")
        if int(give_number) not in guessList:
            guessList.append(int(give_number))

if int(give_number) not in guessList:
    guessList.append(int(give_number))
print ("Congratulations, you were right, the answer was {}! It took you {} tries.".format(dice, len(guessList)))

【讨论】:

  • 对代码进行了改进,现在只计算不同的尝试,包括最后一次。
【解决方案3】:

您也可以将guess_number 变量移到if 语句之外:

    import random

    give_number = input("We will roll a 6 sided dice. What do you think the number will be?\n ")
    guess_number = 0

    dice = random.randint(1,6)
    while give_number != dice:
        guess_number = guess_number + 1
        if give_number > dice:
            give_number = input("Sorry, that answer is too high! Try again!\n ")
            #guess_number = guess_number +1
        if give_number < dice:
            give_number = input("Sorry, that answer is too low! Try again!\n ")
            #guess_number = guess_number +1

    print "Congratulations, you were right, the answer was {}! It took you {} tries.".format(dice,guess_number)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多