Python,为什么它不打印,修复方法是什么?

Python,为什么它不打印,修复方法是什么?,python,Python,当我运行下面的代码时,它会打印单词“None”。它应该打印用户输入的金额。它为什么会这样做,如何修复它 def amount(thing): amnt = int(input('How many ' + thing + ' would you like?')) bolts_amount = amount('bolts') print(bolts_amount) 您需要从函数返回一个值,否则它默认返回None def amount(thing): amnt = int(inpu

当我运行下面的代码时,它会打印单词“None”。它应该打印用户输入的金额。它为什么会这样做,如何修复它

def amount(thing):
    amnt = int(input('How many ' + thing + ' would you like?'))

bolts_amount = amount('bolts')
print(bolts_amount)

您需要
从函数返回一个值,否则它默认返回
None

def amount(thing):
    amnt = int(input('How many ' + thing + ' would you like?'))
    return amnt

bolts_amount = amount('bolts')
print(bolts_amount)
在函数内部局部执行
amnt=int(输入('您想要多少个'+thing+'))
不会影响函数外部的结果。您需要
返回
此值,这样当您调用函数
amount()
时,它将为您指定一个变量
bolts\u amount