Please help. Problem with elif


#1

Hi all,

I'm having trouble finding what is wrong with my elif line. It keeps showing on the terminal that there's and Syntax error on this line. And the reason given is "invalid Syntax". Any clarification please. Thank you!

#The following program is a tool to help you calculate a selected shape.
#Choose a shape and calulate :) 
from math import pi
from time import sleep
from datetime import datetime
now = datetime.now()

#Calculating...be patient!
print "Current time: %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! \nExiting..."

option = raw_input("Enter C for Circle or T for Triangle: ")
option = option.upper()
if option == 'C':
		radius = float(raw_input("Enter radius:"))
		area = pi * radius**2	
print "The pie is baking..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option=='T':
        base = float(raw_input("Enter base:"))
  	    height = float (raw_input("Enter height:"))
        area = (0.5)*base*height
print "Uni Bi Tri..."
sleep (1)
print ("Area: %.2f. \n%s" % (area, hint))

else:
        print "Error! Invalid shape selector specified. Exiting."


#2

elif needs to be after if or another `elif:

if condition:  
     print "do something if condition is true"
elif another condition:
     print "do something for another condition"

what isn't valid, is put code between if and elif:

if condition:  
     print "do something if condition is true"
print "this is invalid"
elif another condition:
     print "do something for another condition"

which is what you did in your code


#3

Thank you. I've got to be more careful with the indent next time. Didn't realize the indent means a lot to Python. Cheers!


#4

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