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

问题描述 投票:0回答:3

我在以下代码中遇到错误,但我不明白它出了什么问题。

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

我不知道出了什么问题或如何解决。

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."
python python-2.5
3个回答
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:

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


0
投票

我认为打印函数 print 应该用作:

print("any string")

不是:

print "any string"
© www.soinside.com 2019 - 2024. All rights reserved.