hpenney2
hpenney2

Reputation: 81

Unable to resolve "SyntaxError: invalid syntax"

I'm very new to Python and I was attempting to make a silly little pointless piece of code. However, when I try to run the code I get an "invalid syntax" error. This is the code:

import time

name=input("What would your name happen to be?")
print("Hello there,", name,"!")

time.sleep(3)

ohno=input("Would you like your PC to crash? (Yes or No)")
if ohno.lower() == "yes":
    print("* crashes", name,"'s PC *")
    else:
        print("Invalid answer, reload script")

if ohno.lower() == "no":
    print("Okay. Have a nice day,", name)
    else:
        print("Invalid answer, reload script")

When the error appears, it doesn't even show where the error is. The syntax seems just fine to me.. or I could just be a complete idiot. Well, if anybody can help me, that would be helpful. Thank you in advance!

Upvotes: 1

Views: 1175

Answers (2)

kiyah
kiyah

Reputation: 1528

Your else statements are out of indent, you should fix them:

import time

name=input("What would your name happen to be?")
print("Hello there,", name,"!")

time.sleep(3)

ohno=input("Would you like your PC to crash? (Yes or No)")
if ohno.lower() == "yes":
    print("* crashes", name,"'s PC *")
else: # change your indenting over here
    print("Invalid answer, reload script")

if ohno.lower() == "no":
    print("Okay. Have a nice day,", name)
else: # and here
    print("Invalid answer, reload script")

Upvotes: 1

Matthias Fripp
Matthias Fripp

Reputation: 18625

I think you are trying to setup a three way branch - one message for "yes", another message for "no", and a different message for any other option. But your code doesn't have the else statements in the right place for this. You could set it up like this:

import time

name=input("What would your name happen to be?")
print("Hello there,", name,"!")

time.sleep(3)

ohno=input("Would you like your PC to crash? (Yes or No)")
if ohno.lower() == "yes":
    print("* crashes", name,"'s PC *")
elif ohno.lower() == "no":
    print("Okay. Have a nice day,", name)
else:
    print("Invalid answer, reload script")

Upvotes: 1

Related Questions