为什么python中的这个猜测代码会一直停止

时间:2017-09-20 16:49:48

标签: python python-2.7

我编写了下面的Python代码,它应该“猜测”1到100之间的数字,你只需告诉它你想到的数字是高还是低。但是出于某些原因,当我尝试播放它时,当我告诉它我的数字更高后告诉它更低或反之亦然时它总是卡住:

import random
import time
import math

correct = 0
goon = 'yes'
biggest = 100
smallest = 1
tries = 10



print 'Hi!'
time.sleep(0.5)
print 'I´m going to try and guess your number'
time.sleep(0.5)
print 'Just tell me if your number is bigger or smaller than what I guessed'
time.sleep(0.5)
print 'And of course you have to tell me when I´m right, ok?'
time.sleep(0.5)
print 'Type "b" if your number is smaller than what I guessed and type "s" if it´s bigger. When I´m right, type "r".'
time.sleep(0.5)
print 'Oh by the way, your number should be between 1 and 100.'

if goon == 'no' or goon == 'No' or goon == 'n':
    print 'Ok, see you soon!'
else:
    while goon == 'yes' or goon == 'Yes' or goon == 'y':

        guess = random.randint(1,100)
        print guess
        answer = raw_input()
        while correct == 0:

            if answer == 'r':
                correct = 1
                endhooray = random.randint(1, 3)
                if endhooray == 1:
                    print 'Yay, I got it!'
                elif endhooray == 2:
                    print 'Finally!'
                elif endhooray == 3:
                    print 'See, I´m good at this!'




            elif answer == 'b':
                smallest = guess
                difference = 100 - guess
                add = random.randint(1, difference)
                guess = guess + add
                if guess < biggest:
                    print guess
                    answer = raw_input()
                elif guess > biggest:
                    while tries == 10:
                        add = random.randint(1, difference)
                        guess = guess + add

                        if guess < biggest:
                            print guess
                            answer = raw_input()
                            tries == 1000000



            elif answer == 's':
                biggest = guess
                difference = guess - 100
                difference = difference * -1 
                subtract = random.randint(1, difference)
                guess = guess - subtract
                if guess > smallest:
                    print guess
                    answer = raw_input()
                elif guess < smallest:
                    while tries == 10:
                        subtract = random.randint(1, difference)
                        guess = guess - subtract

                        if guess > smallest:
                            print guess
                            answer = raw_input()
                            tries = 100000



            else:
                print 'Oops, I don´t know what that means'
                break

1 个答案:

答案 0 :(得分:0)

我冒昧地简化了你的代码。这应该做的工作:

import random
import time

# your intro here    

correct = False
min_, max_ = 0, 100

while not correct:
    guess = random.randint(min_, max_)
    answer = raw_input("Is your number %d? (b/s/r): " % guess)
    if answer == "b":
        min_ = guess+1
        print "Ok, I'll aim higher!"
    elif answer == "s":
        max_ = guess
        print "Ok, I'll aim lower!"
    elif answer == "r":
        hooray = random.choice(["Yay, I got it!", "Finally!", "See, I'm good at this!"])
        print hooray
        correct = True
    else:
        print "Oops, I don't know what that means!"
相关问题