-2

Hi I've recently been asked to do an assignment where I have to create a python program which asks the user to enter their gross pay and then calculates net pay based on a number of deductions. Deductions should be represented as global constants and the program should include a number of functions. I have no problem doing the assignment I just seem to be having difficulty with global constants and I keep getting an error saying that my function isnt defined. This is what I've come up with so far:

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()
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • You defined the variable inside a function which means it can only be accessed within that function. If you want to use it elsewhere, declare it global ( `global G` ) wherever you want to use it. – Inkblot Oct 30 '15 at 15:17
  • 4
    Or do it the proper way and have the functions `return` values. – Morgan Thrapp Oct 30 '15 at 15:18
  • So would I sub the G values I used in the def doDeductions(): function in for global G instead? – UnseenMadness Oct 30 '15 at 15:24

2 Answers2

0

This should work: I put %2f where your variable should go as a form of string formatting. %2f inserts a float rounded to 2 decimal places into a string. Then you would do %("Variable"). The "variable" is the int or float number which cannot be inserted into a string without becoming a string. Another way to turn an integer or float into a string would be str("variable"), but I find string formatting neater. you would do global "variableName" to make a variable global, so you can use it outside the function.

def instructions():
    print ("Hello, welcome to the programme")
    print ("Please follow the onscreen instructions")



def getOutput():
    global G
    G = int(input("Enter gross income: "))
    return G


def displayBreak():
    print("")
    print("Less deductions")
    print("---------------")



def doDeductions():
    global Value
    Value=G*.03
    global Health
    Health=G*.04
    global Pay
    Pay=G*.41
    global Social
    Social=G*.07
    global Net
    Net=G-Value-Health-Pay-Social





    ################################################


instructions()

print("")

getOutput()


displayBreak()

print("")

doDeductions()

print("PRSI                    %2f" %(Value))
print("Health Contrb.          %2f" %(Health))
print("PAYE                    %2f" %(Pay))
print("USC                     %2f" %(Social))
print("")
print("Net Pay                 %2f" %(Net))
print("Programme Complete")

You would insert all of the prints at the bottom after you have called the functions or the variables would be called before they are used and would cause an error in your program.

0

If you define a variable in the main body of code it is a global variable. Local variables are defined within functions, and have limited scope. This means local variables can only be accessed from within the function they're defined in, unlike global variables, which can be accessed anywhere within a program.

You can use the keyword 'global' like this: 'global variable_name' to make a local variable (defined within a function) accessible anywhere.

At the moment, you are doing the exact opposite of what your assignment is asking you. It wants you to use global variables, but you are defining them within functions, making them local. The simple way to correct this is to define these variables outside of a function; in this case, get rid of "def doDeductions():", but leave the variable assignments and unindent them.

Rational Function
  • 425
  • 1
  • 3
  • 10