您好我最近被要求做一个任务,我必须创建一个python程序,要求用户输入他们的毛支付,然后根据扣除数量计算净支付。扣除应表示为全球常数,并且该计划应包含许多功能。 我没有问题做任务我似乎有全局常量的困难,我不断收到一个错误,说我的函数没有定义。这是我想出迄今:Python上的全局常量
def instructions():
print ("Hello, welcome to the programme")
print ("Please follow the onscreen instructions")
def getOutput():
G = int(input("Enter gross income: "))
return G
def displayBreak():
print("")
print("Less deductions")
print("---------------")
def doDeductions():
Value=G*.03
Health=G*.04
Pay=G*.41
Social=G*.07
Net=G-Value-Health-Pay-Social
print("PRSI ",Value)
print("Health Contrb. ",Health)
print("PAYE ",Pay)
print("USC ",Social)
print("")
print("Net Pay ",Net)
print("Programme Complete")
################################################
instructions()
print("")
getOutput()
displayBreak()
print("")
doDeductions()
您在函数内定义了变量,这意味着它只能在该函数内访问。如果你想在其他地方使用它,可以在任何你想使用它的地方声明它是全局的('global G')。 – Inkblot
或者按照正确的方式进行操作,并具有“返回”值功能。 –
那么,我将用于全局G的def doDeductions()函数中的G值代替吗? – UnseenMadness