小白一枚,刚学到函数,请问为什么按照如下代码运行报错:NameError: name 'bonus' is not defined
name=input('请输入员工姓名:')
month=int(input('请输入员工工作时长:'))
def math(month):
if month<6:
bonus=500
elif 6<=month<=12:
bonus=120*month
else:
bonus=180*month
return bonus
math(month)
def conclusion(name,month,bonus):
print(name+'来了'+str(month)+'个月,获得奖金'+str(math(month))+'元。')
conclusion(name,month,bonus)
我把bonus设置成全局变量就不会有这个问题,但是为什么一定要设置成全局变量才可以呢?函数的变量不是可以自己设定叫什么的吗?
name=input('请输入员工姓名:')
month=int(input('请输入员工工作时长:'))
def math(month):
if month<6:
bonus=500
elif 6<=month<=12:
bonus=120*month
else:
bonus=180*month
return bonus
math(month)
def conclusion(name,month,bonus):
print(name+'来了'+str(month)+'个月,获得奖金'+str(math(month))+'元。')
conclusion(name,month,bonus)
我把bonus设置成全局变量就不会有这个问题,但是为什么一定要设置成全局变量才可以呢?函数的变量不是可以自己设定叫什么的吗?