2016-10-01 87 views
1

我對Python相當陌生。我正在製作一臺發電機,讓麪包店估計,如果他們製作紙杯蛋糕,會舉辦一個活動需要多少時間。不順心的事在這裏雖然爲什麼要返回Nonetype?

Batches = print("Batches of Cupcakes:", math.ceil(People * Ingredients/12)) 

Labor = print("Hours of labor:", Batches * 1.25) 

我收到此錯誤:

TypeError: unsupported operand type(s) for *: 'NoneType' and 'float' 
+7

'print'不返回任何東西,所以'Batches'是'NoneType' –

+0

迂腐:從技術上說,' Batches'是'None',它是'NoneType'類型的唯一實例,'Batches'不是它自己的類型。 – ShadowRanger

+0

嘗試:'type(Batches)' –

回答

3

因爲print總是返回無。要保存先前的結果在一個變量:

batches = math.ceil(people * ingredients/12) 
labor = batches * 1.25 

print("Batches of cupcakes:", batches) 
print("Hours of labor:", labor) 
+0

謝謝,這絕對是一個編碼太快的情況。 –

0

print()回報什麼,所以你想要做什麼也沒有乘法,這哪裏是「NoneType」錯誤出現在

如果您想Batches是一個數字,

Batches = math.ceil(People * Ingredients/12) 

打印只輸出文本到Python解釋器:)

整個代碼應是這樣的:

Batches = math.ceil(People * Ingredients/12) 
Labor = Batches * 1.25 

,然後您可以顯示這樣的數據:

print("Batches of cupcakes:", Batches) 
print("Hours of labor:", Labor) 
+1

string + int會導致類型錯誤。我不知道你在說什麼關於一個額外的換行符,打印功能默認插入一個空格。 – L3viathan

+0

@ L3viathan我很抱歉,我糾正了我的錯誤。我經常忘記Python中的字符串和非字符串連接 – AlgoRythm