简体   繁体   English

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

[英]I don't understand what is wrong with this code

I get an error on the following code and I don't understand what is wrong with it. 我在以下代码中收到错误,并且我不明白它的问题所在。

I am just trying to learn how to do this, and this was a test. 我只是想学习如何做,这是一个测试。

I can't figure out what is wrong or how to fix it. 我不知道出什么问题或如何解决。

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."

First error is here : 第一个错误是在这里:

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

What you want to do is a comparison (same for every test case, in your code): 您想要做的是一个比较(代码中的每个测试用例都一样):

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

Also, you want to call the input function : 另外,您想调用输入函数:

answer = input() #Notice the parentheses 

Third error (This is a logical one) : 第三个错误(这是合乎逻辑的):

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

Then again, same error : 再一次,同样的错误:

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:

In addition to what AshRj pointed out, here are two more obvious errors: 除了AshRj指出的以外,还有两个更明显的错误:

answer = input

This will assign the actual function " input " to answer , not call the function. 这将分配实际的功能“ input ”来answer ,而不是调用该功能。 Also, you probably want to use raw_input instead. 另外,您可能想使用raw_input代替。 So, use answer = raw_input() . 因此,请使用answer = raw_input()

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

You're not fetching a new answer between those comparisons, so answer will still be no when you test it again. 您不会在这些比较之间获取新的答案,因此当您再次测试时answer仍然是“ no

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM