+ 2

Area calculator

Why does this keep resulting in an error Traceback (most recent call last): File "file0.py", line 23, in <module> circle() File "file0.py", line 18, in circle radius = diameter / 2 TypeError: unsupported operand type(s) for /: 'str' and 'int' https://code.sololearn.com/czfdRe3Dz929/?ref=app

13th Feb 2023, 12:11 PM
Ethan
Ethan - avatar
2 Respostas
+ 6
input() accept as string type value. Convert to needed type as: diameter = int(input ()) float( input()) # to float type conversion. so you need diameter as float type or int type. edit: choice == "Circle" or "circle": will always returns true. the proper way is to use => if choice == "Circle" or choice == "circle" : similarly others. you can also do as if choice in [ "Circle", "circle" ] :
13th Feb 2023, 12:18 PM
Jayakrishna 🇮🇳
0
# you didn't use type casting strings to integer or str to int try this one, it will work perfectly. print(''' Press (1) to calculate Triangle Press (2) to calculate Rectangle) Press (3) to calculate Circle) ''') choice = int(input('Enter your choice between (1-3): ')) # type cast string input to integer def triangle(): print('You chose to calculate triangle area ') base = int(input('Enter base: ')) # type cast string input to integer height = int(input('Enter height: ')) area = 0.5 * base * height print("Area = " + str(area)) # type cast area is an integer need to convert it to string def rectangle(): print('You chose to calculate rectangle area ') length = int(input('Enter length: ')) width = int(input('Enter width: ')) area = length * width print("Area = " ,area) # or use , if u want to avoid type cast def circle(): print('You chose to calculate circle area ') diameter = int(input('Enter diameter: ')) radius = diameter / 2 area = str(3.14 * radius * radius) print("Area = " ,area) if choice == 1: triangle() elif choice == 2: rectangle() elif choice == 3: circle()
14th Feb 2023, 11:39 PM
Avishek Chatterjee
Avishek Chatterjee - avatar