PYTHON - 反向数字猜测游戏 -

时间:2013-02-15 01:43:56

标签: python

所以我一直试图找出一种方法来编写一个程序,计算机试图猜测我正在考虑的数字,而不是猜测计算机所选数字的另一种方式。它在大多数情况下工作,但是在某些情况下,它确实在链中重复数字,即使我之前已经告诉它,例如我想的值高于'7'。在某些情况下,即使我告诉它更高或更低,它也会重复相同的数字。如果有经验的人可以看看这个,并告诉我在这些循环中我缺少什么,这将有很大的帮助。

#computer enters a value x
#lower - computer guesses lower than x
#higher - computer guesses higher than x
#when string "You got it!" - game over

import random

lowBound = 0
highBound = 100
randomNumber = random.randint(lowBound,highBound)

print ("Is it ", randomNumber, " ?")
response = input()

while response != "You got it!":
    if response == "higher":
        lowBound = randomNumber    
        randomNumber = random.randint (lowBound, highBound)
        print ("Is it ", randomNumber, " ?")
        response = input()

    elif response == "lower":
        highBound = randomNumber
        randomNumber = random.randint (lowBound, highBound)
        print ("Is it ", randomNumber, " ?")
        response = input()

    if response == "You got it!":

        print ("Woohooo, I'm so bitchin'")

5 个答案:

答案 0 :(得分:3)

random.randint具有包容性,因此:

if response == 'higher':
    lowBound = randomNumber + 1

if response == 'lower':
    highBound = randomNumber - 1

此外,如果用户没有输入有效的回复,则永远不会再次调用input(),程序将在无限循环中挂起。

更强大,但不处理骗子:

import random

lowBound = 0
highBound = 100
response = ''
randomNumber = random.randint(lowBound,highBound)

while response != "yes":
    print ("Is it ", randomNumber, " ?")
    response = input()
    if response == "higher":
        lowBound = randomNumber + 1   
        randomNumber = random.randint(lowBound,highBound)
    elif response == "lower":
        highBound = randomNumber - 1
        randomNumber = random.randint(lowBound,highBound)
    elif response == "yes":
        print ("Woohooo, I'm so bitchin'")
        break
    else:
        print ('Huh? "higher", "lower", or "yes" are valid responses.')

答案 1 :(得分:1)

random.randint(a, b)会在ab之间返回一个数字。生成新的随机数时,您应该使用random.randint(lowBound+1, highBound-1)

答案 2 :(得分:0)

你提到的其中一个问题就是这些问题:

highBound = randomNumber
randomNumber = random.randint (lowBound, highBound)

你正在设置一个新的界限,这很好,但是你要选择另一个随机数!

你应该做的是将界限减半,并从那里要求用户更高或更低。看看二进制搜索算法。

highBound = randomNumber
randomNumber = randomNumber / 2

您的程序仍然可以正常工作(此处提及的其他更改),但这会在大多数情况下更快地猜测您的号码。

实际上有an example of this game on Wikipedia.

答案 3 :(得分:0)

这是Michael Dawson的书中我练习的这个练习版本,我试图尽量减少计算机使用的尝试次数。我知道代码看起来很狡猾,这只是我的第二天:)


answer=""
guess=50
counter=3
x=25

print("hi, guess the number from 1 too 100")
input("\n")

print ("i will try to guess it")
print ("is it ", guess, "?")

while answer not in ("y","l","s"):
    print ("sorry, i didn't understand \n")
    answer=input("type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")

if answer in ("s","l"):
    while answer!="y":

        if answer=="l":
            guess=int(guess-x)
            print ("how about", guess,"?")
            answer=input("\nis it? type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")
            x=100/2**counter
            counter=counter+1
            if x<1:
                x=1

        elif answer=="s":
            guess=int(guess+x)
            print ("how about", guess,"?")
            answer=input("\nis it? type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")
            x=100/2**counter
            counter=counter+1
            if x<1:
                x=1

        elif answer=="y":
            break
else:
    pass

print("\ngreat! the number that you guessed is", guess)
print("i can read your mind with no guesses!")
input("\n")

答案 4 :(得分:-1)

你得到两次数字,因为random.randint的边界是包容性的; random.randint(1, 3)可以返回1,2或3.请注意,如果响应既不是“更高”,也不是“更低”,也不是“你得到它”,你还应该继续询问人:

import random
lowBound = 0
highBound = 100

while True:
    randomNumber = random.randint(lowBound, highBound)
    print ("Is it ", randomNumber, " ?")
    response = input()

    if response == "higher":
        lowBound = randomNumber + 1
    elif response == "lower":
        highBound = randomNumber - 1

    if response == "You got it!":
        print ("Woohooo, I'm so bitchin'")
        break