亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法連接“ str”和“ float”對象?

無法連接“ str”和“ float”對象?

HUWWW 2019-12-06 11:05:14
我們的幾何老師給我們分配了一個作業,要求我們創建一個玩具在現實生活中何時使用幾何的示例,因此我認為編寫一個程序來計算填充一定數量的水池需要多少加侖水會很酷。形狀,并具有一定的尺寸。這是到目前為止的程序:import easyguieasygui.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我該怎么辦?
查看完整描述

3 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

在連接之前,必須將所有浮點數或非字符串數據類型強制轉換為字符串


這應該可以正常工作:(請注意str強制轉換為乘法結果)


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

直接來自口譯員:


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


查看完整回答
反對 回復 2019-12-06
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

使用Python3.6 +,您可以使用f字符串格式化打印語句。


radius=24.0

height=15.0

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


查看完整回答
反對 回復 2019-12-06
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

還有另一種解決方案,您可以使用字符串格式設置(類似于我猜的C語言)


這樣,您也可以控制精度。


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)

沒有精度


您需要27129.600000加侖水來填充該池。


精度8.2


您需要27129.60加侖的水來填充該池。


查看完整回答
反對 回復 2019-12-06
  • 3 回答
  • 0 關注
  • 613 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號