Wednesday, 15 January 2014

Confused with global constants (python) -



Confused with global constants (python) -

hi problem given

"write python programme asks user come in gross pay , calculates net pay based on next deductions;  prsi 3%,  health contribution 4%  paye 41%  u.s.c. 7% hint: deductions should represented global constants. programme should include number of functions i.e. function display instructions user; function takes input user , calls 3rd function calculates net pay (i.e. gross pay – deductions)"

here effort

def instructions(): print ("hello, , welcome programme") def getoutput(): g = int(input("enter gross income: ")) homecoming 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()

this "dodeductions" not work know wrong have no thought why

any help appreciated,

thanks

the problem you're not storing value, health, pay, social, or net anywhere within function. these values exist duration of dodeductions() method, , within scope. not exist 1 time chain of print() statements, 1 of reasons programme not run - asking print non-existent objects.

note you're ignoring 1 of mandates of assignment, store deductions global constants.

what should do, outside context of class, store global values:

prsi = 0.03 health_contribution = 0.04 paye = 0.41 usc = 0.07 g = 0 value = 0 health = 0 pay = 0 social = 0 net = 0 def getoutput(): dodeductions(int(input("enter gross income: "))) def dodeductions(g): global value, health, pay, social, net, prsi, health_contribution, paye, usc value = g * prsi health = g * health_contribution pay = g * paye social = g * usc net = g - value - health - pay - social

the global keyword used state interpreter variable defining in non-global scope should assigned global variable.

this ugly way of doing this, though, , 1 of advantages of classes can have members can access self.member, rather needing phone call global every single time refer variable in top scope.

python

No comments:

Post a Comment