Why am I getting a syntax error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anderson1373
    New Member
    • Nov 2009
    • 23

    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.

    Code:
    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!")
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.
    Code:
    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!")

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      I See Three Errors

      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!

      Comment

      • anderson1373
        New Member
        • Nov 2009
        • 23

        #4
        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.

        Comment

        • anderson1373
          New Member
          • Nov 2009
          • 23

          #5
          Now it is not printing my menu. How do I call that function?
          line 14

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            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:
            Code:
            show_menu()
            Luck!

            Comment

            • anderson1373
              New Member
              • Nov 2009
              • 23

              #7
              thank you so much for your help!

              Comment

              Working...