0

this image shows how every input results in execution in triangleCalc() function block 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')
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Mureinik Jul 29 '18 at 06:40
  • you could reference some code like using argparse: https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse – Charles Cao Jul 29 '18 at 06:46

2 Answers2

0

It may seem redundant to a beginning programmer, but if option == 't' or 'T' should actually be written as if option == 't' or option == 'T'.

On a side note, strings (like 'T') evaluate to True in python. So option == 't' or 'T' will always be True, regardless of what option == 't' evaluates to.

Brian
  • 823
  • 6
  • 14
  • 1
    wow. that just solved my problem and I was scratching my head on it from past 2 hours, thank you! the code runs perfectly fine now. @bstrauch24 – mystical_starseed Jul 29 '18 at 06:44
0

as @bstrauch24 explains where you are going wrong I would like to add

If you have to use various combinations or input then every time comparison is not good then go for in operator

option = input()
if option in ['t','T']:
    triangleCalc()
elif option in ['c','C']:
    circleCalc()
Onk_r
  • 836
  • 4
  • 21