443,846 Members | 1,872 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

# Why am I getting a syntax error

 P: 23 This is giving me a syntax error at the elif statement but i'm not sure why. please help me. Expand|Select|Wrap|Line Numbers miles = 0.0 kilometers = 0.0 def show_menu():     print("1. Convert miles into kilometers.")     print("2. Convert kilometers into miles.")     print("3. Exit")   def convert_miles_to_kilometers(miles):     miles* 1.609     return kilometers def convert_kilometers_to_miles(kilometers):         kilometers * 0.621     return miles show_menu choice = int(raw_input("Enter a choice [1-3]: "))     if choice == 1:     miles = float(raw_input("How many miles? "))     kilometers = convert_miles_to_kilometers(miles)     print ("%f miles is %f kilometers." %  (miles, kilometers) elif choice == 2:     kilometers = float(raw_input("How many kilometers? "))     miles = convert_kilometers_to_miles(kilometers)     print("%f kilometers is %f miles." % (kilometers, miles)) else:     print("Good-bye!")   Nov 8 '10 #1

#### ✓ answered by Oralloy

anderson,

I'm reading your code, not putting it through the compiler, so I might have missed some things.

Lines 9 and 12 look like they should be assignments to `kilometers` and `miles`, respectively.

Also, it looks as if line 19 is missing a closing parenthesis.

Please, next time you post a question about errors, post the error message, too. I'm pretty sure that python told you exactly where your errors were, so go back and read the messages and see if they make sense.

Luck!

6 Replies

 Expert 100+ P: 624 First, we would want to look at the error message itself, so we don't have to go through all possible errors and see if any apply. Second, you always want to check the previous line as well before posting. If you don't have it formatted properly, the interpreter can think that this line is a continuation of the previous line. Note that there are other syntax errors in the program as well. You should test each function as you create it, before going on to code the next function. Expand|Select|Wrap|Line Numbers miles = 0.0 kilometers = 0.0 def show_menu():     print("1. Convert miles into kilometers.")     print("2. Convert kilometers into miles.")     print("3. Exit")   def convert_miles_to_kilometers(miles):     miles* 1.609     return kilometers def convert_kilometers_to_miles(kilometers):         kilometers * 0.621     return miles show_menu choice = int(raw_input("Enter a choice [1-3]: "))     if choice == 1:     miles = float(raw_input("How many miles? "))     kilometers = convert_miles_to_kilometers(miles)     print ("%f miles is %f kilometers." %  (miles, kilometers)     #     # if you get an error message for this line, instead of     # the "elif", you know the error is on the previous line     print("test message") elif choice == 2:     kilometers = float(raw_input("How many kilometers? "))     miles = convert_kilometers_to_miles(kilometers)     print("%f kilometers is %f miles." % (kilometers, miles)) else:     print("Good-bye!") Nov 8 '10 #2

 Expert 100+ P: 983 anderson, I'm reading your code, not putting it through the compiler, so I might have missed some things. Lines 9 and 12 look like they should be assignments to `kilometers` and `miles`, respectively. Also, it looks as if line 19 is missing a closing parenthesis. Please, next time you post a question about errors, post the error message, too. I'm pretty sure that python told you exactly where your errors were, so go back and read the messages and see if they make sense. The more information you give us, the more we can help you. Luck! Nov 8 '10 #3

 P: 23 Thank you so much I have been going over and over the code and didnt see the ) on line 19. The only thing it said was syntax error and it was showing line 20. Nov 8 '10 #4

 P: 23 Now it is not printing my menu. How do I call that function? line 14 Nov 8 '10 #5

 Expert 100+ P: 983 No worries - in many languages, a syntax error will often show up on the line following the actual line in error. This is because they expect the statement to be completed, and it isn't; instead, a strange token appears where it isn't expected. Since the error showing the incomplete statement is the first thing on the following line, that is where the error is flagged at .... does that make sense? I think you need to use a pair of parentheses behind your call to `show_menu` at line 14: Expand|Select|Wrap|Line Numbers show_menu() Luck! Nov 8 '10 #6

 P: 23 thank you so much for your help! Nov 8 '10 #7