Python猜测游戏:防止Python猜测相同的数字

时间:2015-10-10 20:27:43

标签: python

我正在使用python中的猜谜游戏,我想我只拥有一切,我想让程序猜测它已经猜到的数字,例如,如果用户数是5,它选择3用户输入'+'并且它知道数字更高,并且如果程序猜测6用户输入' - '并且它知道数字低于6,但有时它猜测2,显然如果数字高于3它不可能是2,所以我怎么写呢?我是这方面的初学者,如果你能做到这一点,我将不胜感激,下面是我的代码。

 print("Hello,")
 print("welcome to the guessing game")
 print('I shall guess a number between 1 and 99, and then ask you if am right')
 print('I have a maximum of 20 chances\n')

import random
guess = random.randint(1,99)
print("Your number is %f, Am i right?" % guess)
print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
ans = input('Which is it: ')
print("You chose %s" % ans)
minguess = 1
maxguess = 99
count = 0

while (count < 20):
    count = count + 1
    if ans == '+':
##I am using these prints to keep track of the numbers and if everything is working correctly
        maxguess1 = guess + 1
        print('THe maxguess is', maxguess1)
        newguess = random.randint(maxguess1, maxguess)
        print('The newguess is', newguess)
        newguess = int(newguess)
        print("Is it %d?" % newguess)
        print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
        ans = input('Which is it: ')
    elif ans == "-":
        maxguess2 = guess - 1
        print('The minus maxguess is', maxguess2)
        newguess = random.randint(minguess, maxguess2)
        print('The minus newguess is', newguess)
        newguess1 = int(newguess)
        print("Is it %d?" % newguess1)
        print('If I am, enter =, If the number is higher enter (+), if the number is lower enter (-)')
        ans = input('Which is it: ')

    if ans == "=":
        print('YAAAAAAS MAN')

我希望它在猜到新号码时改变数字

    guess = newguess

1 个答案:

答案 0 :(得分:0)

注意:此示例在Python 2.7中,而不是Python 3,但概念是相同的。

将问题分解为各个元素:

$collection -> update(
    array('_id' => 3, "data._id" => 2),
    array('$push' =>
        array('data.$.userid' => 52)
    )
);