【发布时间】:2015-10-30 15:15:23
【问题描述】:
嗨,我最近被要求做一个任务,我必须创建一个 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),无论您想在哪里使用它。 -
或者以正确的方式进行操作并具有
return值的功能。 -
那么我是否可以将我在 def doDeductions(): function in for global G 中使用的 G 值子化?
标签: python variables constants global python-3.3