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

use*_*922 0 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."
Run Code Online (Sandbox Code Playgroud)

ash*_*shr 5

第一个错误在这里:

if answer = "yes": #This would be giving you a syntax error
Run Code Online (Sandbox Code Playgroud)

你想要做的是比较(对于每个测试用例,在你的代码中都是如此):

if answer == "yes": #Notice the double equals to sign
Run Code Online (Sandbox Code Playgroud)

另外,您想调用输入函数:

answer = input() #Notice the parentheses 
Run Code Online (Sandbox Code Playgroud)

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

print "Very well, would you like to play a guessing game?"
#You are missing an input statemtent
if answer = "yes":
Run Code Online (Sandbox Code Playgroud)

然后,同样的错误:

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:
Run Code Online (Sandbox Code Playgroud)

  • 实际上,它始终是Python中的语法错误. (3认同)