SyntaxError : invalid syntax


#1



https://www.codecademy.com/en/courses/python-ext/projects/area-calculator?user_id=56fca2a51026d97ad000045c

It gives me this error : "SyntaxError : invalid syntax" at line 19

I expect elif to run.

"""This program is a calculator that will compute the area of a given shape, as selected by the user. The calculator can determine that area of circles and triangles."""
from math import pi
from time import sleep
from datetime import datetime
now = datetime.now()
print "The calculator is starting up."
print '%s/%s/%s %s:%s' % (now.month, now.day, now.year, now.hour, now.minute)
sleep(1)
hint = "Don't forget to include the correct units!"
print "Enter C for Circle or T for Triangle: "
option = raw_input("Enter C for Circle or T for Triangle: ")
option = option.upper()
if option == 'C':
	radius = float(raw_input("Input a radius: "))
	area = (radius ** 2) * pi
	print "The pie is baking. . . "
	sleep (1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
	base = float(raw_input("Input a base: "))
	height = float(raw_input("Input a height: "))
	area = base * height
	print "Uni Bi Tri. . . "
	sleep (1)
print ("Area: %.2f. \n%s" % (area, hint))
else:
	print "Invalid input. Terminating program. . . "
	sleep(2)
	quit()


#2

Hi
The reason why python is not happy with your elif is because the previous line is not indented. Thus the opening if is implicitly closed and that 's why there is an error with the elif. The same problem occurs at line 26 with the else.

By the way, the surface of a triangle is base * height / 2 :slight_smile:

Cheerio


#3

Hello,

when I run my code I obtain:

$ python AreaCalculator.py
File "AreaCalculator.py", line 19
print ("Area: %.2f. \n%s" % (area,hint))
^
IndentationError: unexpected indent

My line 19 is the print() before the elif. I don't understand what is wrong since the print is properly indented.

if option == 'C':
      radius = float(raw_input("Enter the radius:"))
      ...
      sleep(1)
      print ("Area: %.2f. \n%s" % (area,hint))
elif option == 'T':
      base = float(raw_input("Enter the base:"))
      ...
      sleep(1)
      print ("Area is = %.2f. \n %s" % (area,hint))

#4

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.