我不明白这段代码有什么问题

时间:2012-12-31 03:53:28

标签: python python-2.5

我在下面的代码中遇到错误,我不明白它有什么问题。

我只是想学习如何做到这一点,这是一个考验。

我无法弄清楚出了什么问题或如何解决它。

print "Would you like to see today's weather?"

answer = input

if answer = "yes":
    print "Follow Link: http://www.weather.com/weather/right-now/Yorktown+VA+23693 "
elif answer = "no":
    print "Very well, would you like to play a guessing game?"
    if answer = "yes":
        import random

        secret = random.randint (1, 99)
        guess= 0
        tries= 0

        print "AHOY!  I'm the Dread Pirate Roberts, and I have a secret!"
        print "It is a number from 1 to 99. I'll give you 6 tries. "

        while guess != secret and tries < 6:
            guess = input("What's your guess? ")
            if guess < secret:
                print "Too low, ye scurvy dog!"
            elif guess > secret:
                print "Too high, landlubber!"
            tries = tries + 1
            if guess == secret:
                print "Avast! Ye got it! Found my secret ye did!"
    elif answer = "no":
        print "Thank you, and goodnight."

2 个答案:

答案 0 :(得分:5)

第一个错误在这里:

if answer = "yes": #This would be giving you a syntax error

您要做的是比较(对于每个测试用例,在您的代码中都是相同的):

if answer == "yes": #Notice the double equals to sign

另外,你要调用输入函数:

answer = input() #Notice the parentheses 

第三次错误(这是合乎逻辑的错误):

print "Very well, would you like to play a guessing game?"
#You are missing an input statemtent
if answer = "yes":

然后,同样的错误:

print "It is a number from 1 to 99. I'll give you 6 tries. "
#You are agin missing an input statement
while guess != secret and tries < 6:

答案 1 :(得分:3)

除了AshRj所指出的,还有两个更明显的错误:

answer = input

这会将实际功能“input”分配给answer,而不是调用该功能。此外,您可能希望使用raw_input。因此,请使用answer = raw_input()

elif answer = "no":
    print "Very well, would you like to play a guessing game?"
    if answer = "yes":

您没有在这些比较之间获取新的答案,因此当您再次测试时,answer仍然是no