<p>将<code>print</code>替换为<code>return</code>,您就可以:</p>
<pre><code>def potato_quant(number):
if number >= 5 and number < 100:
return("You've got more than 5 potatoes. Making a big meal eh?")
elif number >= 100 and number < 10000:
return("You've got more than 100 potatoes! What do you need all those potatoes for?")
elif number >= 10000 and number < 40000:
return("You've got more than 10000! Nobody needs that many potatoes!")
elif number >= 40000:
return("You've got more than 40000 potatoes. wut.")
elif number == 0:
return("No potatoes eh?")
elif number < 5:
return("You've got less than five potatoes. A few potatoes go a long way.")
else:
return '0'
def potato_type(variety):
if variety == "Red":
return("You have chosen Red potatoes.")
elif variety == "Sweet":
return("You have chosen the tasty sweet potato.")
elif variety == "Russet":
return("You've got the tasty average joe potatoe!")
else:
return("Nice potato!")
print(potato_quant(15000) + potato_type("Sweet"))
</code></pre>
<p>在第<code>print(potato_quant(15000) + potato_type("Sweet"))</code>行中,您调用的函数<code>potato_quant</code>&;<code>potato_type</code>必须返回要打印的值。</p>
<p>因为您的函数有<code>print</code>而不是<code>return</code>,所以它们返回<code>None</code>,并且操作数<code>+</code>在<code>None</code>上未定义。因此出现错误消息。</p>
<p>如果<code>potato_quant</code>返回<code>0</code>,它将提高<code>TypeError: unsupported operand type(s) for +: 'int' and 'str'</code>。所以我把<code>return 0</code>替换为<code>replace '0'</code>来解决这个问题。</p>