user3033788
user3033788

Reputation: 11

Python If/Else Statements

i am a bit of a newbie when it comes to python...

however i cannot get this piece of code to work. I have spent quite a while trying to figure out what is going wrong, in various books and question sites so i came here.

My python idle(Python GUI) (The Software i am using) Keeps on coming up with a syntax error when ever i use an Else statement...

a = str
print("What is Your name")
a = input()
b = str
print("Oh Hello " + a)
print("How Are You Feeling")
b = input()
if b == "Happy":
    print("Glad to hear that":
          else:
          print("Oh im sorry to hear that")

Sorry that it is a conversation WITH A COMPUTER but i never got to choose the topic so i could not help it...

Please help

Upvotes: 0

Views: 166

Answers (2)

chepner
chepner

Reputation: 530970

Indentation matters in Python. The else must be indented exactly the same amount as the if it pairs with:

a = str
print("What is Your name")
a = input()
b = str
print("Oh Hello " + a)
print("How Are You Feeling")
b = input()
if b == "Happy":
    print("Glad to hear that")   # Added the missing ) here as well
else:
    print("Oh im sorry to hear that")

Upvotes: 7

jramirez
jramirez

Reputation: 8685

The else statement needs to have the same indentation as its corresponding if. You are also missing a ) at the end of your print and you don't need that : at the end of your print.

if b == "Happy":
    print("Glad to hear that")  # <== missing that last ), you don't need that :
else:  # <== indentation was wrong here
    print("Oh im sorry to hear that")

Upvotes: 2

Related Questions