您如何编写一个程序来计算出矩形的面积,方法是从用户输入中收集矩形的高度和宽度,计算面积并显示结果?您将如何处理长方体的体积?到目前为止,我的代码是:(我刚刚开始使用python)
shape = input("> ")
height = input("Please enter the height: ")
width = input("please enter the width: ")
area = [height*width]
print ("The area is", 'area')
但是我收到无效的语法。
在Python 3.x中,input
返回一个字符串。因此,height
和width
都是字符串。
area = [height*width]
您在这里将字符串相乘并创建一个列表。您需要将它们转换为整数(带有int
函数)或浮点数(带有float
函数),像这样
height = float(input("Please enter the height: "))
width = float(input("please enter the width: "))
...
area = height*width
然后,最print
好像这样将单个字符串传递给函数
print ("The area is {}".format(area))
或者您可以简单地打印像这样的项目
print ("The area is", area)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句