Question:
What's causing python syntax error in if/else block?
2011-03-22 20:57:00 UTC
I keep getting a syntax error on the first print statement below and I have no idea why. Can anyone help? Thanks!

# Prompt the user for a password and check it against "hello"
if str(raw_input("Please enter your password: ") == "hello"

# if the user entered "hello" ask their name and respond based on input
if raw_input("Please enter your name: ") == "Diane"
print "What a great name!"
elif str(raw_input("Please enter your name: ")) == "Charlene"
print "May I have your autograph, please?"
elif str(raw_input("Please enter your name: ")) == "Agatha"
print "May I have your autograph, please?"
else
print str_name + ", that's a nice name."
print "Thank you for using Program 3 - Loops and If conditions!"
Three answers:
Jack Trades
2011-03-23 07:07:50 UTC
You must end each if line with a colon (:) AND have the next line indented with the expression to be executed if the test is true.



Example:



if True:

. print "hello"



--- or ---



if "name" == "name":

. print "hello"



Also it would be better to only ask for input once, then check the input against different names...



name = raw_input("Please enter your name: ")



if name == "Diane":

. print "What a great name!"

elif name == "Charlene" or name == "Agatha":

. print "May I have your autograph, please?"

else:

. print name + ", that's a nice name."



***** You will have to replace the periods above with spaces.
?
2016-11-16 13:19:51 UTC
i do no longer know why it is happening inspite of the shown fact that it is happeneing to me to so i anticipate it is something incorrect with the full internet site! I logged on and had a couplequestions to respond to and have been given the comparable message. provide it an hour and spot if its lower back to favourite!
chrism
2011-03-22 21:09:59 UTC
paste the code into a paste bin so that we can see the indentation. Otherwise, we can't help because python is interpreted by the indentation.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...