python - 函数中的 Python 错误“netpay 未定义”
问题描述
我不断收到上面的错误,我的猜测是我没有正确传递信息
BASE_PAY=900
total_sale=float(input('What\'s your total sale?: '))
def main():
demographics=get_info()
income=get_income()
budget=get_budget(netpay)
print('Total sales is: $',total_sale)
print('Your comission is: $',comission)
print('The gross pay is: $',Gpay)
print('The deductions are: $',deductions)
print('The netpay is: $',netpay)
print('Housing & Utility: $', HnC)
print('Food & Clothing: $', FnC)
print('Entertainment: $', entertainment)
print('Miscellaneous costs: $', misc)
def get_info():
Fname=input('Enter your first name: ')
Lname=input('Enter your last name: ')
gender=input('Please enter your gender(m/f): ')
if gender=='m' or gender =='M':
print('Mr.',Fname,Lname)
else:
print('Ms.',Fname,Lname)
return Fname, Lname, gender
def get_income():
comission=total_sale*0.06
Gpay=BASE_PAY*comission
deductions=Gpay*0.18
netpay=Gpay-deductions
return comission, Gpay, deductions, netpay
def get_budget(netpay):
HnC=netpay*0.45
FnC=netpay*0.20
entertainment=netpay*0.25
misc=netpay*0.10
return Hnc,FnC, entertainment, misc
main()
解决方案
你还没有netpay
为你的函数定义get_budget
,你已经在本地的另一个函数中定义了它get_income
,所以在你试图调用它的地方,它是看不到的。您应该创建一个名为的全局变量netpay
并将其声明为 None。然后你可以从你的函数内部编辑它并在你的get_income
函数中调用它get_budget
而不返回这个错误。
也许通读一下以了解python中的变量范围。https://www.w3schools.com/python/python_scope.asp
推荐阅读
- nuget-package - 从 GitHub 包注册表安装 NuGet 包
- wordpress - 检查变量是否为真后如何启用 WordPress 插件激活
- c# - 在 Scriban 模板引擎中替换字典值
- java - Spring JPA 无法从类型 [java.lang.String] 转换为类型 [java.lang.Long]?
- html - 为什么我的垂直导航隐藏在其余元素后面?
- android - 检查 firebase 查询(对于 firebase 选项)是否返回空(不存在)
- c# - ELM327 发送多个命令时不返回任何内容
- c - Linux/POSIX ABI 是否定义 int 的大小?
- .net - GitHub上DotNet repro中“@dotnet-bot test this please”背后的逻辑是什么?
- amazon-web-services - aws lambda 和 Rds 中的握手不活动超时错误