我对Python还是很陌生,我已经写了我的第一个代码来计算形状区域。每次我在Jupyter上运行它时,都会说
类型错误。
我哪里弄错了? (我正在使用Python 3)。
# Calculator code for the area of the shape
print("1 rectangle")
print ("2 squared")
print ("3 circle")
shape = input (">> ")
if shape ==1:
length = input ("What is the length of the rectangle?")
breadth = input ("What is the breadth of the rectangle?")
area = length * breadth *2
print ("the area of your rectangle", area)
elif shape == 2:
length = input ("what is the length of one side of the squared")
area = length *breadth
print ("the area of your square is ", area)
else shape ==3:
radius = input ("what is the radius of your circle")
area = radius *radius*3.14
print ("area of your circle is ", area)
答案 0 :(得分:0)
您的代码有误:
float
,以进行更好的计算。else
语法。请参见 documentation 尝试以下代码:
print("1 rectangle")
print("2 squared")
print("3 circle")
shape = input (">> ")
if shape == 1:
length = float(input ("What is the length of the rectangle?"))
breadth = float(input ("What is the breadth of the rectangle?"))
area = length * breadth *2
print ("the area of your rectangle", area)
elif shape == 2:
length = float(input ("what is the length of one side of the squared"))
area = length * length
print ("the area of your square is ", area)
else:
radius = float(input ("what is the radius of your circle"))
area = radius *radius*3.14
print ("area of your circle is ", area)
希望这能回答您的问题!