samedi 28 juillet 2018

How do I get input from user and call a function based on the input?

I'm new to Python and while practicing I made this program, It asks the user to choose one of the two shapes among circle and triangle. But every time I enter an input, no matter if it's a 'c', 't', 'r' or anything else, the function to calculate triangle area gets executed.

'''
This is a Calculator program
which asks the user to select a shape
and then calculate its area based on given dimensions
'''
print ('Shape Area Calculator is now running')

def triangleCalc():
    base = float(input('Enter Base of triangle: '))
    height = float(input('Enter Height of triangle: '))
    areaT = 0.5 * base * height
    print ('The area of triangle is: ' + str(areaT))

def circleCalc():
     radius = float(input('Enter radius of Circle: '))
     areaC = 3.14159 * radius * radius
     print ('The area of Circle is ' + str(areaC))



print('Which shape would you like to calculate the Area of?')
print('Enter C for Circle or T for Triangle')
option = input()
if option == 't' or 'T':
    triangleCalc()
elif option == 'c'or 'C':
    circleCalc()
else:
    print ('Invalid Choice')

Aucun commentaire:

Enregistrer un commentaire