1
  1. Write a Python program which accepts the radius of a circle from the user and compute the area. Go to the editor Sample Output : r = 1.1 Area = 3.8013271108436504

My code:

def rad_to_area(rad):
    from math import pi
    area = pi * (rad**2)
    return area
radius = input('Enter radius of circle: ')
print('The area of the circle is', rad_to_area(radius))

Error:

Traceback (most recent call last):
  File "/tmp/sessions/167965d8c6c342dd/main.py", line 6, in <module>
    print('The area of the circle is', rad_to_area(radius))
  File "/tmp/sessions/167965d8c6c342dd/main.py", line 3, in rad_to_area
    area = pi * (rad**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Could someone please explain what I am doing wrong?

Thanks

Deepak47
  • 9
  • 1
  • 3

1 Answers1

0

The input method is a string and to do power operations on the radius you need to cast it as a float first.

def rad_to_area(rad):
    from math import pi
    area = pi * (rad**2)
    return area
radius = float(input('Enter radius of circle: ')) #Casting as a float here
print('The area of the circle is', rad_to_area(radius))
Edeki Okoh
  • 1,786
  • 15
  • 27