Web Analytics Made Easy -
StatCounter Elif 'invalid syntax' - CodingForum

Announcement

Collapse
No announcement yet.

Elif 'invalid syntax'

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Elif 'invalid syntax'

    Code:
    import time
    print("The current time is", time.strftime("%H:%M"), ".")
    
    print("How may I help you? ")
    
    print("====================================")
    print("| [01] Addition                    |")
    print("| [02] Subtraction                 |")
    print("| [03] Multiplication              |")
    print("| [04] Division                    |")
    print("| [05] Square a number             |")
    print("| [06] Cube a number               |")
    print("| [07] Find the area of a circle   |")
    print("| [08] Find the volume of a sphere |")
    print("| [09] Count                       |")
    print("====================================")
    
    request = input()
    
    if request == ("1"):
        number1 = float(input("Please give me number. "))
        number2 = float(input("Now the the second number. "))
        number3 = number1 + number2
        print ((number1),"plus",(number2),"is",(number3),".")
        
    elif request == ("2"):
        number1 = float(input("Please give me number. "))
        number2 = float(input("Now the the second number. "))
        number3 = number1 + number2
        print ((number1),"minus",(number2),"is",(number3),".")
    
    elif request == ("3"):
        number1 = float(input("Please give me number. "))
        number2 = float(input("Now the the second number. "))
        number3 = number1 * number2
        print ((number1),"times",(number2),"is",(number3),".")
        
    elif request == ("4"):
        number1 = float(input("Please give me number. "))
        number2 = float(input("Now the the second number. "))
        number3 = number1 / number2
        print (((number1),"divided by",(number2),"is",(number3),".")
    
    elif request == ("5"):
        number = float(input("What would you like me to square? "))
        squarenumber = number * number
        print ((number),"is",(squarenumber),"squared.")
    
    elif request == ("6"):
        number = float(input("What would you like me to cube? "))
        cubednumber = number * number * number
        print (((mumber),"is",(cubednumber),"cubed.")
    
    elif request == ("7"):
        radius = float(input("What is the circle's radius? "))
        pi = 3.14159265359
        area = pi * radius * radius
        print ("The area of a circle with a radius of", (radius), "is" (area)".")
    
    elif request == ("8"):
        radius = float(input("What is the sphere's radius? "))
        pi = 3.14159265359
        volume = 4/3 * pi * radius * radius * radius
        print ("The volume of a circle with a radius of", (radius), "is" (volume)".")
    
    elif request == ("9"):
        i = 0
        while True:
            add = int(input())
            i += (add)
            print ("The running total is",(i),".")
                       
    else:
        print("Huh? Type one of the numbers from the menu.")

    This is my code. When I try to run it, it tells me that I have used incorrect syntax, on this part: elif request == ("5"): and all of those after it.

    Could somebody help, because this seems like a common problem, yet I've found no actual answers.
    Last edited by vinyl-junkie; Dec 17, 2016, 03:11 PM. Reason: added code tags

  • #2
    It's all correctly indented on the actual program.

    Comment


    • #3
      You have an incorrect number of parenthesis by ( if request == "4" ). You're also missing various data type separators in your print statements -- meaning you're missing "," to separate your integers, floats and strings in some of the print statements.

      Comment


      • #4
        You are having the wrong string concatenation.

        The following soution of else if loop will solve your syntax error :

        Code:
        if request == '1':
                number1 = input("Please give me number. ")
                number2 = input("Now the the second number. ")
                number3 = float(number1) + float(number2)
                print(number3)
                print(str(number1)+" plus "+str(number2)+" is "+str(number3)+".")
        
        elif request == '2':
                number1 = input("Please give me number. ")
                number2 = input("Now the the second number. ")
                number3 = float(number1) - float(number2)
                print (str(number1)+" minus "+str(number2)+" is "+str(number3)+".")
        
        elif request == '3':
               number1 = input("Please give me number. ")
               number2 = input("Now the the second number. ")
               number3 = float(number1) * float(number2)
               print (str(number1)+" times "+str(number2)+" is "+str(number3)+".")
        
        elif request == '4':
               number1 = input("Please give me number. ")
               number2 = input("Now the the second number. ")
               number3 = float(number1) / float(number2)
               print (str(number1)+" divided by "+str(number2)+" is "+str(number3)+".")
        
        elif request == '5':
               number = input("What would you like me to square? ")
               squarenumber = float(number) * float(number)
               print (str(number)+" is "+str(squarenumber)+" squared.")
        
        elif request == '6':
               number = input("What would you like me to cube? ")
               cubednumber = float(number) * float(number) * float(number)
               print (str(number)+" is "+str(cubednumber)+" cubed.")
        
        elif request == '7':
               radius = input("What is the circle's radius? ")
               pi = 3.14159265359
               area = float(pi) * float(radius) * float(radius)
               print ("The area of a circle with a radius of "+str(radius)+" is "+str(area)+".")
        
        elif request == '8':
               radius = input("What is the sphere's radius? ")
               pi = 3.14159265359
               volume = 4/3 * pi * float(radius) * float(radius) * float(radius)
               print ("The volume of a circle with a radius of "+str(radius)+" is "+str(volume)+".")
        
        elif request == '9':
                i = 0
                while True:
                      add = int(input())
                       i += (add)
                       print ("The running total is "+i+".")
        
        else:
                print("Huh? Type one of the numbers from the menu.")
        ASP.Net Software Development

        Comment

        Working...
        X