简体   繁体   中英

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

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

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

answer = input() #Notice the parentheses 

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

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:

answer = input

This will assign the actual function " input " to answer , not call the function. Also, you probably want to use raw_input instead. So, use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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