By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ 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
  1. miles = 0.0
  2. kilometers = 0.0
  3. def show_menu():
  4.     print("1. Convert miles into kilometers.")
  5.     print("2. Convert kilometers into miles.")
  6.     print("3. Exit")
  7.  
  8. def convert_miles_to_kilometers(miles):
  9.     miles* 1.609
  10.     return kilometers
  11. def convert_kilometers_to_miles(kilometers):    
  12.     kilometers * 0.621
  13.     return miles
  14. show_menu
  15. choice = int(raw_input("Enter a choice [1-3]: "))    
  16. if choice == 1:
  17.     miles = float(raw_input("How many miles? "))
  18.     kilometers = convert_miles_to_kilometers(miles)
  19.     print ("%f miles is %f kilometers." %  (miles, kilometers)
  20. elif choice == 2:
  21.     kilometers = float(raw_input("How many kilometers? "))
  22.     miles = convert_kilometers_to_miles(kilometers)
  23.     print("%f kilometers is %f miles." % (kilometers, miles))
  24. else:
  25.     print("Good-bye!")
  26.  
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.

The more information you give us, the more we can help you.

Luck!

Share this Question
Share on Google+
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
  1. miles = 0.0
  2. kilometers = 0.0
  3. def show_menu():
  4.     print("1. Convert miles into kilometers.")
  5.     print("2. Convert kilometers into miles.")
  6.     print("3. Exit")
  7.  
  8. def convert_miles_to_kilometers(miles):
  9.     miles* 1.609
  10.     return kilometers
  11. def convert_kilometers_to_miles(kilometers):    
  12.     kilometers * 0.621
  13.     return miles
  14. show_menu
  15. choice = int(raw_input("Enter a choice [1-3]: "))    
  16. if choice == 1:
  17.     miles = float(raw_input("How many miles? "))
  18.     kilometers = convert_miles_to_kilometers(miles)
  19.     print ("%f miles is %f kilometers." %  (miles, kilometers)
  20.     #
  21.     # if you get an error message for this line, instead of
  22.     # the "elif", you know the error is on the previous line
  23.     print("test message")
  24. elif choice == 2:
  25.     kilometers = float(raw_input("How many kilometers? "))
  26.     miles = convert_kilometers_to_miles(kilometers)
  27.     print("%f kilometers is %f miles." % (kilometers, miles))
  28. else:
  29.     print("Good-bye!")
Nov 8 '10 #2

Oralloy
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

Oralloy
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
  1. show_menu()
Luck!
Nov 8 '10 #6

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

Post your reply

Sign in to post your reply or Sign up for a free account.