2016-10-01 110 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