Invalid syntax with else in an if-elif-else statement

def rightTriangle ():
length = float (input ("What is the length of the triangle you want to measure? "))
width = float (input ("What is the width of the triangle you want to measure? "))
area = 0.5 * length * width
perimeter = float ((((length * length) + (width * width)) ** 0.5) + length + width)
return area
return perimeter

def equalateralTriangle ():
length = float (input ("What is the length of one of the sides of the triangle? "))
area = 5 * length (length * 3**.5)
perimeter = length * 3
return area
return perimeter

def parallelgoram ():
length = float (input ("What is the length of a side of the parallelogram you want to measure? "))
width = float (input ("What is the width of the parallelgoram you want to measure? "))
area = length * width
perimeter = 2 * (length + width)
return area
return perimeter

def rectangle ():
length = float (input("What is the length of the rectangle you want to measure? "))
width = float (input ("What is the width of the rectangle you want to measure? "))
area = length * width
perimeter = 2 * (length + width)
return area
return perimeter

def square ():
length = float (input("What is the length of one of the sides you want to measure? "))
area = length * length
perimeter = 4 * length
return area
return perimeter

def circle ():
radius = float(input("What is the radius of the triangle you want to measure? "))
area = (radius * radius) * 3.14159
perimeter = 2 * 3.14159 * radius
return area
return perimeter

def Main ():
response = input ("The avaiable shapes for finding area and perimeter are: \n (1) Right Triangle \n (2) Equalateral Triangle \n (3) Parallelogram \n (4) Rectangle \n (5) Square \n (6) Circle \nWhich shape do you want to choose? ")

def rightTriangle (): length = float (input ("What is the length of the triangle you want to measure? ")) width = float (input ("What is the width of the triangle you want to measure? ")) area = 0.5 * length * width perimeter = float ((((length * length) + (width * width)) ** 0.5) + length + width) return area return perimeter def equalateralTriangle (): length = float (input ("What is the length of one of the sides of the triangle? ")) area = 5 * length (length * 3**.5) perimeter = length * 3 return area return perimeter def parallelgoram (): length = float (input ("What is the length of a side of the parallelogram you want to measure? ")) width = float (input ("What is the width of the parallelgoram you want to measure? ")) area = length * width perimeter = 2 * (length + width) return area return perimeter def rectangle (): length = float (input("What is the length of the rectangle you want to measure? ")) width = float (input ("What is the width of the rectangle you want to measure? ")) area = length * width perimeter = 2 * (length + width) return area return perimeter def square (): length = float (input("What is the length of one of the sides you want to measure? ")) area = length * length perimeter = 4 * length return area return perimeter def circle (): radius = float(input("What is the radius of the triangle you want to measure? ")) area = (radius * radius) * 3.14159 perimeter = 2 * 3.14159 * radius return area return perimeter def Main (): response = input ("The avaiable shapes for finding area and perimeter are: \n (1) Right Triangle \n (2) Equalateral Triangle \n (3) Parallelogram \n (4) Rectangle \n (5) Square \n (6) Circle \nWhich shape do you want to choose? ") if response == "1": area = rightTriangle () print ("The area of your right triangle is " + str(area) + ". \n" "The perimeter of your right triangle is " + str(perimeter) + ". ") elif response == "2": area = equalateralTriangle () print ("The area for your equalateral triangle is " + str(area) + ". \n" + "The perimeter for your equalateral triangle is " + str(perimeter) + ". ") elif response == "3": area = parallelgoram () print ("The area for your parallelogram is " + str(area) + ". \n" + "The perimeter for your parallelogram is " + str(perimeter) + ". ") elif response == "4": area = rectangle () print ("The area for your rectangle is " + str(area) + ". \n" "The area for your rectangle is " + str(perimeter) + ". ") elif response == "5": area = square () print ("The area for your square is " + str(area) + ". \n" + "The perimeter for your square is " + str(perimeter) + ". ") elif response == "6": area = circle () print ("The area for your circle is " + str(area) + ". \n" + ("The perimeter for your circle is " + str(perimeter) + ". ") else: print (" You made an incorrect desicion. Please choose the options based through the numbers 1-6.") Main () again = input("Do you want to find the area or perimeter of another shape?") if again == "yes" or "Yes": Main () length = 0 ; width = 0 ; area = 0 ; radius = 0 ; perimeter = 0 print ("This is a program designed to ease all life's troubles with solving the area and perimeter of multiple shapes. For this program, enter the number beside the shape you want to find the area an and the perimeter of. \n") Main ()

if response == “1”:
area = rightTriangle ()
print ("The area of your right triangle is " + str(area) + “. \n” "The perimeter of your right triangle is " + str(perimeter) + ". ")

elif response == “2”:
area = equalateralTriangle ()
print ("The area for your equalateral triangle is " + str(area) + “. \n” + "The perimeter for your equalateral triangle is " + str(perimeter) + ". ")

elif response == “3”:
area = parallelgoram ()
print ("The area for your parallelogram is " + str(area) + “. \n” + "The perimeter for your parallelogram is " + str(perimeter) + ". ")

elif response == “4”:
area = rectangle ()
print ("The area for your rectangle is " + str(area) + “. \n” "The area for your rectangle is " + str(perimeter) + ". ")

elif response == “5”:
area = square ()
print ("The area for your square is " + str(area) + “. \n” + "The perimeter for your square is " + str(perimeter) + ". ")

elif response == “6”:
area = circle ()
print ("The area for your circle is " + str(area) + “. \n” + ("The perimeter for your circle is " + str(perimeter) + “. “)
else:
print (” You made an incorrect desicion. Please choose the options based through the numbers 1-6.”)
Main ()
again = input(“Do you want to find the area or perimeter of another shape?”)
if again == “yes” or “Yes”:
Main ()

length = 0 ; width = 0 ; area = 0 ; radius = 0 ; perimeter = 0
print (“This is a program designed to ease all life’s troubles with solving the area and perimeter of multiple shapes. For this program, enter the number beside the shape you want to find the area an and the perimeter of. \n”)
Main ()

Usually with an error like this, if the syntax error is not on the line indicated, it is on the preceding code. Check that line (particularly for missing parenthesis).

You had an extra ( on the previous line.
You are also missing a * in one of the previous functions.

To avoid the main function calling itself, you could use a while loop.

here’s a version using a loop and simulating inputs:

sample_input = "1,2,3,yes,2,4,yes,3,5,6,yes,4,7,8,yes,5,9,yes,6,10,no".split(",") sample_index = 0 def input(string): global sample_index print(string) print("sample input >>>", end=" ") used = sample_input[sample_index] print(used) sample_index += 1 return used def rightTriangle (): length = float (input ("What is the length of the triangle you want to measure? ")) width = float (input ("What is the width of the triangle you want to measure? ")) area = 0.5 * length * width perimeter = float ((((length * length) + (width * width)) ** 0.5) + length + width) return area return perimeter def equalateralTriangle (): length = float (input ("What is the length of one of the sides of the triangle? ")) area = 5 * length * (length * 3**.5) perimeter = length * 3 return area return perimeter def parallelgoram (): length = float (input ("What is the length of a side of the parallelogram you want to measure? ")) width = float (input ("What is the width of the parallelgoram you want to measure? ")) area = length * width perimeter = 2 * (length + width) return area return perimeter def rectangle (): length = float (input("What is the length of the rectangle you want to measure? ")) width = float (input ("What is the width of the rectangle you want to measure? ")) area = length * width perimeter = 2 * (length + width) return area return perimeter def square (): length = float (input("What is the length of one of the sides you want to measure? ")) area = length * length perimeter = 4 * length return area return perimeter def circle (): radius = float(input("What is the radius of the triangle you want to measure? ")) area = (radius * radius) * 3.14159 perimeter = 2 * 3.14159 * radius return area return perimeter def Main (): again = "Yes" while again == "Yes" or again == "yes": response = input ("The avaiable shapes for finding area and perimeter are: \n (1) Right Triangle \n (2) Equalateral Triangle \n (3) Parallelogram \n (4) Rectangle \n (5) Square \n (6) Circle \nWhich shape do you want to choose? ") if response == "1": area = rightTriangle () print ("The area of your right triangle is " + str(area) + ". \n" "The perimeter of your right triangle is " + str(perimeter) + ". ") elif response == "2": area = equalateralTriangle () print ("The area for your equalateral triangle is " + str(area) + ". \n" + "The perimeter for your equalateral triangle is " + str(perimeter) + ". ") elif response == "3": area = parallelgoram () print ("The area for your parallelogram is " + str(area) + ". \n" + "The perimeter for your parallelogram is " + str(perimeter) + ". ") elif response == "4": area = rectangle () print ("The area for your rectangle is " + str(area) + ". \n" "The area for your rectangle is " + str(perimeter) + ". ") elif response == "5": area = square () print ("The area for your square is " + str(area) + ". \n" + "The perimeter for your square is " + str(perimeter) + ". ") elif response == "6": area = circle () print ("The area for your circle is " + str(area) + ". \n" + "The perimeter for your circle is " + str(perimeter) + ". ") else: print (" You made an incorrect desicion. Please choose the options based through the numbers 1-6.") #Main () again = input("Do you want to find the area or perimeter of another shape?") #if again == "yes" or "Yes": #Main () length = 0 ; width = 0 ; area = 0 ; radius = 0 ; perimeter = 0 print ("This is a program designed to ease all life's troubles with solving the area and perimeter of multiple shapes. For this program, enter the number beside the shape you want to find the area an and the perimeter of. \n") Main ()

What’s with all the double returns?

Only the first return statement will work here:

If you really need to return two values, you could return a tuple.
Like this:

def square ():
  length = float (input("What is the length of one of the sides you want to measure? "))
  area = length * length
  perimeter = 4 * length
  return (area, perimeter)

and then using that would look like this:

(area, perimeter) = square ()
print ("The area for your square is " + str(area) + ". \n" + "The perimeter for your square is " + str(perimeter) + ". ")

Sequences are automatically tuples so we don’t need the parens on the return.

return area, perimeter

at the caller we wouldn’t necessarily want to unpack into a tuple but rather keep the variables exposed.

#  pretend '1' is input
print (square())
(1.0, 4.0)    #  already a tuple

area, perimeter = square()    # unpacking the tuple

The code now correctly runs but now I have the problem of the perimeter not working. Is there any possibly you guys can help.

You should post your updated code, did you address the double return issue? When a function executes a return, that function is finished.

def one_return (): return "This is returned" return "This is not returned" print("This is not printed") print(one_return())

This is the updated code that works.

def rightTriangle (): length = float (input ("What is the length of the triangle you want to measure? ")) width = float (input ("What is the width of the triangle you want to measure? ")) area = 0.5 * length * width perimeter = float ((((length * length) + (width * width)) * 0.5) + length + width) return (area, perimeter) def equalateralTriangle (): length = float (input ("What is the length of one of the sides of the triangle? ")) area = 0.25 * length (length * 3**.5) perimeter = length * 3 return (area, perimeter) def parallelgoram (): length = float (input ("What is the length of a side of the parallelogram you want to measure? ")) width = float (input ("What is the width of the parallelgoram you want to measure? ")) area = length * width perimeter = 2 * (length + width) return (area, perimeter) def rectangle (): length = float (input("What is the length of the rectangle you want to measure? ")) width = float (input ("What is the width of the rectangle you want to measure? ")) area = length * width perimeter = 2 * (length + width) return area return perimeter def square (): length = float (input("What is the length of one of the sides you want to measure? ")) area = length * length perimeter = 4 * length return (area, perimeter) def circle (): radius = float(input("What is the radius of the triangle you want to measure? ")) area = (radius * radius) * 3.14159 perimeter = 2 * 3.14159 * radius return (area, perimeter) def Main (): response = str(input ("The avaiable shapes for finding area and perimeter are: \n (1) Right Triangle \n (2) Equalateral Triangle \n (3) Parallelogram \n (4) Rectangle \n (5) Square \n (6) Circle \nWhich shape do you want to choose? ")) if response == "1": area = rightTriangle () print ("The area of your right triangle is " + str(area) + ". \n" "The perimeter of your right triangle is " + str(perimeter) + ". ") elif response == "2": area = equalateralTriangle () print ("The area for your equalateral triangle is " + str(area) + ". \n" + "The perimeter for your equalateral triangle is " + str(perimeter) + ". ") elif response == "3": area = parallelgoram () print ("The area for your parallelogram is " + str(area) + ". \n" + "The perimeter for your parallelogram is " + str(perimeter) + ". ") elif response == "4": area = rectangle () print ("The area for your rectangle is " + str(area) + ". \n" "The area for your rectangle is " + str(perimeter) + ". ") elif response == "5": area = square () print ("The area for your square is " + str(area) + ". \n" + "The perimeter for your square is " + str(perimeter) + ". ") elif response == "6": area = circle () print ("The area for your circle is " + str(area) + ". " + ("\nThe perimeter for your circle is " + str(perimeter) + ". ")) else: print (" You made an incorrect desicion. Please choose the options based through the numbers 1-6.") Main () again = input("Do you want to find the area or perimeter of another shape? ") if again == "yes" or "Yes": Main () length = 0 ; width = 0 ; area = 0 ; radius = 0 ; perimeter = 0 print ("This is a program designed to ease all life's troubles with solving the area and perimeter of multiple shapes. For this program, enter the number beside the shape you want to find the area an and the perimeter of. \n") Main ()

Missed the returns at lines 26 and 27.

Note that Python interprets any sequence,

'a', 2, True, 42

as a tuple so we don’t need parens on the return. The sequence will be returned as a tuple which we can store intact at the caller, or unpack into individual variables.

2 Likes

Thank you it all works now.

2 Likes