Python 无法连接“str”和“float”对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16948256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:04:01  来源:igfitidea点击:

Cannot concatenate 'str' and 'float' objects?

pythonstringconcatenation

提问by user2443381

Our geometry teacher gave us an assignment asking us to create an example of when toy use geometry in real life, so I thought it would be cool to make a program that calculates how many gallons of water will be needed to fill a pool of a certain shape, and with certain dimensions.

我们的几何老师给了我们一个作业,要求我们创建一个玩具在现实生活中何时使用几何的例子,所以我认为制作一个程序来计算需要多少加仑的水来填充特定的水池会很酷。形状,并具有一定的尺寸。

Here is the program so far:

这是到目前为止的程序:

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

i keep getting this error though:

我不断收到此错误:

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

what do i do?

我该怎么办?

回答by Kalyan02

All floats or non string data types must be casted to strings before concatenation

所有浮点数或非字符串数据类型必须在连接前转换为字符串

This should work correctly: (notice the strcast for multiplication result)

这应该可以正常工作:(注意str乘法结果的转换)

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")

straight from the interpreter:

直接来自口译员:

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.

回答by Gaurang Shah

There is one more solution, You can use string formatting (similar to c language I guess)

还有一种解决方案,您可以使用字符串格式(我猜类似于 c 语言)

This way you can control the precision as well.

这样你也可以控制精度。

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

without precision

不精确

You need 27129.600000 gallons of water to fill this pool.

您需要 27129.600000 加仑的水来填满这个水池。

With precision 8.2

精度 8.2

You need 27129.60 gallons of water to fill this pool.

您需要 27129.60 加仑的水来填满这个水池。

回答by Tyberius

With Python3.6+, you can use f-stringsto format print statements.

使用 Python3.6+,您可以使用f-strings来格式化打印语句。

radius=24.0
height=15.0
print(f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool.")