Python猜谜游戏,计算猜测次数

时间:2015-04-01 18:05:48

标签: python count dice

我的计算机科学入门老师给了我们一个骰子猜测游戏的挑战,虽然我已经设法弄清楚了大部分组件,但最后一件事是返回正确的数字(一旦他们'已经猜到了)以及他们想出来的尝试次数。我的代码会返回所有这些内容,但如果猜测的人猜两次相同的数字,则不会解释。有没有办法告诉他们在忽略任何重复数字的同时找到一个数字需要多少猜测?

这是我到目前为止的代码:

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)

3 个答案:

答案 0 :(得分:1)

我建议使用一个列表来附加猜测。我以这种方式更改了代码(对于我的情况下的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)))

答案 1 :(得分:0)

为了检测重复的猜测,我们必须跟踪所有先前的猜测。

我们只需要知道之前是否已经猜到了一个特定的数字 - 而不是它已经出现了多少次。

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

以空集开头(而不是以guess_number = 1开头),然后将每个猜测添加到此集。

一旦这个人猜对了,你可以检查一下这个集的大小 - 这是猜测的数量,忽略重复。

答案 2 :(得分:0)

你也可以将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)
相关问题