user1938922
user1938922

Reputation: 25

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

Upvotes: 0

Views: 175

Answers (2)

asheeshr
asheeshr

Reputation: 4114

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:

Upvotes: 5

Dolda2000
Dolda2000

Reputation: 25855

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.

Upvotes: 3

Related Questions