473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Why am I getting a syntax error

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!

6 2562
dwblas
626 Expert 512MB
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
988 Expert 512MB
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
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
Now it is not printing my menu. How do I call that function?
line 14
Nov 8 '10 #5
Oralloy
988 Expert 512MB
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
thank you so much for your help!
Nov 8 '10 #7

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

Similar topics

3
by: kufre | last post by:
I need some fresh eyes to take a look at this code for me and let me know what it is I'm doing wrong because I keep getting all kinds of error message with this code. I've had several people help...
2
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable...
5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
14
by: group8perl | last post by:
am getting syntax error, can someone please explain how to insert a hyperlink into a table.
18
by: rahuls | last post by:
Hi everyone, I have a couple of questions? 1.this is part of my main function ////////////////////////////// int main() { FILE *f1; int i,j; j=100000000; int intData;
1
by: ruvi | last post by:
Hi, I am using Vb 6 and Access. I am getting syntax error in the following sql query. strSql = "SELECT ...
3
by: silambu | last post by:
hi,can anybody tell reason for getting syntax error near , while either updating or inserting records in table
3
paulrajj
by: paulrajj | last post by:
hi to all, i am getting syntax error on my code.. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in D:\xampp\htdocs\Dummy\paulraj\matrim\exam.php on line 62 ...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
1
by: inbarik | last post by:
Hi, i'm getting syntax error message for this button that tries to get name (of building) with apostrophe. Any idea how to solve this? Note that i must use name with apostrophe e.g. O'neal: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.