Function 函数中不支持的操作数类型 编写一个Python程序,接受用户提供的圆半径并计算面积。去找编辑 样本输出: r=1.1 面积=3.8013271108436504

Function 函数中不支持的操作数类型 编写一个Python程序,接受用户提供的圆半径并计算面积。去找编辑 样本输出: r=1.1 面积=3.8013271108436504,function,types,operands,Function,Types,Operands,我的代码: 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)) 错误: Traceback (most recent call last): File "/tmp/

我的代码:

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))
错误:

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'
回溯(最近一次呼叫最后一次):
文件“/tmp/sessions/167965d8c6c342dd/main.py”,第6行,在
打印('圆的面积为',半径到面积(半径))
文件“/tmp/sessions/167965d8c6c342dd/main.py”,第3行,在rad_-to_区域
面积=π*(拉德**2)
TypeError:不支持**或pow()的操作数类型:'str'和'int'
有人能解释一下我做错了什么吗


多亏了,
输入方法是一个字符串,要在半径上执行幂运算,首先需要将其转换为浮点

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))

input
方法是一个字符串,要对半径执行幂运算,首先需要将其转换为浮点

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。你花了多长时间才精通?怀疑现在开始进入如果你想你可以看看我的个人资料,并在那里的任何一个链接消息我面对面交谈。