我目前正在通過一個蟒蛇教程,要求我創建一個隨機函數並運行它10種不同的方式。我被困在如何真正讓它使用浮動。我想我應該張貼了整個事情,只是指出在那裏我試圖讓漂浮工作我需要知道如何讓我的函數返回浮動。我很困惑我在哪裏把浮標
def volume (length, width, height):
print "the object is %d cm long, %d cm wide, and %d cm high" % (length, width, height),
total_volume = float(length * width * height)
print "The total volumeis %d cm3" % total_volume
print "Direct input:"
volume (10, 20, 30)
print "direct input variables:"
length = (10)
width = (20)
height = (30)
volume (length, width, height)
print "direct input variables and math"
volume (length + 10, width +20, height +30)
print "direct input math"
volume (10 + 10, 20 +20, 30 + 30)
print "user input with int(raw_input)"
length2 = int(raw_input("what is the length? "))
width2 = int(raw_input("what is the width? "))
height2 = int(raw_input("what is the height? "))
volume (length2, width2, height2)
#here is the first problem
print "user input with float(raw_input)"
length3 = float(raw_input("what is the length? "))
width3 = float (raw_input("what is the width? "))
height3 = float (raw_input("what is the height? "))
volume (length3, width3, height3)
#Doesn't work here either`
print "float(raw_input) + variables"
print "the base oject size is 10 * 10 * 10"
print "why is this important? IT ISN'T!!!!!"
print "However, eventually I will make one that calculates the increase in volume"
length4 = length + float(raw_input("How much length are you adding? "))
width4 = width + float(raw_input("How much width are you adding? "))
height4 = height + float(raw_input("How much height are you adding? "))
volume (length4, width4, height4)
這兩部分簡單地拒絕返回浮動。這是我到目前爲止所嘗試的。
我嘗試添加該函數變量調用時浮,如下
量浮動(length4,width4,寬度4)
我試圖浮動添加到函數的實際定義部分如下
DEF體積浮子(長度,寬度,高度):
,你可以看到,我有浮動放置在該函數的實際的數學部分,沒有效果。
它必須是可能的,使這項工作。我希望有人更有知識可以指出方向,我不知道
使用'return'語句和你想要返回的任何值(變量):'return total_volume'。 – Evert
爲了記錄:Python不是C語言或類似語言:不需要聲明類型(但可以[提示類型](https://docs.python.org/3/library/typing.html) Python 3.5)。 – Evert
並不是說你也不會有後果:在函數中將'total_volume'強制轉換爲浮點數,然後使用'%d'(整數)說明符將其打印出來。 – Evert