我们的几何老师给我们布置了一个作业,要求我们创造一个玩具在现实生活中使用几何的例子,所以我认为制作一个程序来计算一个特定形状和特定尺寸的水池需要多少加仑的水是很酷的。
目前的计划是:
import easygui
easygui.msgbox("This program will help determine how many gallons will be needed to fill up a pool based off of the dimensions given.")
pool=easygui.buttonbox("What is the shape of the pool?",
choices=['square/rectangle','circle'])
if pool=='circle':
height=easygui.enterbox("How deep is the pool?")
radius=easygui.enterbox("What is the distance between the edge of the pool and the center of the pool (radius)?")
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
不过,我一直在犯这个错误:
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))
+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
我该怎么办?
所有浮点或非字符串数据类型必须在连接前强制转换为字符串
这应该可以正常工作:(注意乘法结果的
str
强制转换)直接从口译员那里:
使用Python3.6+,可以使用f-strings格式化打印语句。
还有一个解决方案,您可以使用字符串格式(我猜类似于c语言)
这样你也可以控制精度。
不精确
精度为8.2
相关问题 更多 >
编程相关推荐