用Python中的用户输入填充函数参数



我正试图创建一段代码,该代码可以执行简单的减法运算,并根据用户的输入打印消息,但我还不知道如何让用户输入正确地用作函数参数。

我一直收到以下错误,尽管我在用户输入的同时包含了int:

类型错误:'>='在>'的实例之间不支持NoneType'和'int'

你知道我遗漏了什么吗?

我正在处理的确切代码如下所示:

def withdraw_money(current_balance, amount):
if (current_balance >= amount):
current_balance = current_balance - amount
return current_balance
balance = withdraw_money(int(input("How much money do you have")), int(input("How much do your groceries cost")))
if (balance >= 0): 
print("You're good")
else: 
print("You're broke")

您在else 中缺少一个return

因此,每当你崩溃时,你的函数什么都不返回,你什么都不比较(None(

def withdraw_money(current_balance, amount):
if (current_balance >= amount):
current_balance = current_balance - amount
return current_balance
else: # need to return something if the above condition is False
return current_balance - amount
balance = withdraw_money(int(input("How much money do you have")), int(input("How much do your groceries cost")))
if (balance >= 0): 
print("You're good")
else: 
print("You're broke")

PS:如果你在函数外检查这个人是否破产,为什么要在函数内检查差异?(你的fnc可以返回差额,然后在外面你可以检查它是-ive还是+ive(

最新更新