Reputation: 51
Hope you are all well.
Trying to create a Python program which acts as a dictionary however now having some issues with creating an elif statement. Im my IDLE I keep getting signs saying that my syntax is wrong for the elif, I am not quite what I am doing wrong though? I suppose it is an indentation error but what exactly is it?
if choice == "0":
print "good bye"
elif choice == "1":
name = raw_input("Which philosopher do you want to get")
if name in philosopher:
country = philosopher [name]
print name, "comes from" , country
else:
print "No such term"
***elif choice == "2" :*** ***<<I am being told that I have syntax error in this elif element, what am I doing wrong)**
name = raw_input(" What name would you like to enter")
if name not in philosopher:
country = raw_input( "Which country do you want to put your philosopher in")
philosopher [name] = country
print name, "has now been added and he is from", country
else:
print "We already have that name"
Upvotes: 0
Views: 98
Reputation: 11
I think that you are correct as to an indentation problem. Here is what I think you are trying to do:
if choice == "0":
print "good bye"
elif choice == "1":
name = raw_input("Which philosopher do you want to get")
if name in philosopher:
country = philosopher [name]
print name, "comes from" , country
else:
print "No such term"
elif choice == "2" :
name = raw_input(" What name would you like to enter")
if name not in philosopher:
country = raw_input( "Which country do you want to put your philosopher in")
philosopher [name] = country
print name, "has now been added and he is from", country
else:
print "We already have that name"
The key problem is inconsistent indentation, which makes it hard for Python to determine what you want. Until you develop your own style and have a good reason for doing otherwise, a consistent four spaces of indentation per level is a good habit. Let your editor help you indent consistently. Oh, and make sure not to mix tabs and spaces when you indent: that has a way of seeming to work for a bit and then coming back and biting you.
Upvotes: 1
Reputation: 2349
It looks like you want to put your if name in philosopher
...No such term"
section inside of the block beginning elif choice == "1":
. If so, you need to indent one additional time in order for Python to properly group your if
, elif
and else
statements.
if choice == "0":
# code
elif choice == "1":
if name in philospher: # indented twice; can only run if choice == "1"
# code
else:
# code
elif choice == "2":
# code
# rest of code
Upvotes: 0
Reputation: 1289
Assuming you fix the indentation, the if statements all go in this order for you:
if x:
#do something
elif x:
#do something
if x:
#do something
else:
#do something
elif x:#CAUSES ERROR
#do something
if x:
#do something
else:
#do something
Your elif
comes AFTER an else
statement. You can't do this. elif
MUST go between if
and else
. Otherwise the compiler doesn't ever catch the elif
(Because it just ran through and did the else
statement). In other words, you must have your if statements ordered like so:
if x:
#do something
elif x:
#do something
else:
#do something
Upvotes: 2