计算器程序问题

时间:2019-02-28 22:53:43

标签: python python-3.x

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

1 个答案:

答案 0 :(得分:0)

您的代码有误:

  1. 您尚未为输入内容提供任何类型。将其替换为float,以进行更好的计算。
  2. 您编写了错误的else语法。请参见 documentation
  3. 您错误地计算了平方面积。

尝试以下代码:

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)

希望这能回答您的问题!