Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python Functions

  • 25-10-2016 6:13pm
    #1
    Banned (with Prison Access) Posts: 613 ✭✭✭


    Ok guys just started using python and Im trying to figure out how to call them properly as instructions given by my lecturer.

    #These Are The Tax Deductions And Are Global Variables
    prsi = 0.03
    health_contribution = 0.04
    paye = .41
    usc = 0.07
    #This Is The Main Function
    def main():
    def welcome():
    print("Welcome To Your Wage Calculator")
    print("Follow The On Screen Instructions To Find Your Final Earnings ")
    welcome()
    earnings = float(input("Enter Earnings Here: "))
    #This Is Where We Calculate The Deductions Of The Wages From Gross Pay with the function wage_calc
    def wage_calc():
    print("")
    print("Gross Pay: €", ("%.2f"%earnings))
    print("")
    print("Less Deductions")
    print("
    ")
    print("PRSI: €", ("%.2f"%(earnings*prsi)))
    print("Health Contribution: €", ("%.2f"%(earnings*health_contribution)))
    print("PAYE: €", ("%.2f"%(earnings*paye)))
    print("USC: €", ("%.2f"%(earnings*usc)))
    print("")
    #This Is Where We Call These Deductions Into The Programme
    wage_calc()
    #This Is Where The Final Wage Is Calculated & Displayed im the function final_wage
    def final_wage():
    print("Net Pay: €", ("%.2f"%(earnings-((earnings*prsi)+(earnings*health_contribution)+(earnings*paye)+(earnings*usc)))))
    print("Programme Completed")
    final_wage()
    #This Is Where We Call The Main Programme To Run
    main()

    This is how I currently run it and it works. But I think Im calling it wrong. Does the functions not have to be in the main function then defined outside the function. When I do this I keep getting errors though


Comments

  • Registered Users, Registered Users 2 Posts: 6,282 ✭✭✭Talisman


    Indentation is important in Python, it determines what block of code a statement belongs to.

    I would expect the main function to be formatted as below. The earnings value needs to be passed to the wage_calc and final_wage functions.
    def main():
            welcome()
            earnings = float(input("Enter Earnings Here: "))
            wage_calc(earnings)
            final_wage(earnings)
    


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Talisman wrote: »
    Indentation is important in Python, it determines what block of code a statement belongs to.

    I would expect the main function to be formatted as below. The earnings value needs to be passed to the wage_calc and final_wage functions.
    def main():
            welcome()
            earnings = float(input("Enter Earnings Here: "))
            wage_calc(earnings)
            final_wage(earnings)
    

    I have the indentations alright, just didnt copy correctly. Even doing it your way didnt work. We havent covered it in class either which means we arent allowed to do it. There must be some simple person way that Im missing. Thanks for the help buddy


  • Registered Users, Registered Users 2 Posts: 6,282 ✭✭✭Talisman


    Kal El wrote: »
    I have the indentations alright, just didnt copy correctly. Even doing it your way didnt work. We havent covered it in class either which means we arent allowed to do it. There must be some simple person way that Im missing. Thanks for the help buddy
    If you're not allowed to pass the parameter to the functions then earnings must be a global variable. In Python variables exist only in the first scope where they're assigned.

    Add the following line after the declaration of the other global variables:
    earnings = None
    


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭pillphil


    This is just your own code with the code tags around it.
    #These Are The Tax Deductions And Are Global Variables
    prsi = 0.03
    health_contribution = 0.04
    paye = .41
    usc = 0.07
    #This Is The Main Function
    def main():
        def welcome():
            print("Welcome To Your Wage Calculator")
            print("Follow The On Screen Instructions To Find Your Final Earnings ")
        welcome()
        earnings = float(input("Enter Earnings Here: "))
    #This Is Where We Calculate The Deductions Of The Wages From Gross Pay with the function wage_calc
        def wage_calc():
            print("")
            print("Gross Pay: €", ("%.2f"%earnings))
            print("")
            print("Less Deductions")
            print("---------------")
            print("PRSI: €", ("%.2f"%(earnings*prsi)))
            print("Health Contribution: €", ("%.2f"%(earnings*health_contribution)))
            print("PAYE: €", ("%.2f"%(earnings*paye)))
            print("USC: €", ("%.2f"%(earnings*usc)))
            print("")
    #This Is Where We Call These Deductions Into The Programme
        wage_calc()
    #This Is Where The Final Wage Is Calculated & Displayed im the function final_wage
        def final_wage():
            print("Net Pay: €", ("%.2f"%(earnings-((earnings*prsi)+(earnings*health_contribution)+(earnings*paye)+(earnings*usc)))))
            print("Programme Completed")
        final_wage()
    #This Is Where We Call The Main Programme To Run
    main()
    


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    pillphil wrote: »
    This is just your own code with the code tags around it.
    #These Are The Tax Deductions And Are Global Variables
    prsi = 0.03
    health_contribution = 0.04
    paye = .41
    usc = 0.07
    #This Is The Main Function
    def main():
        def welcome():
            print("Welcome To Your Wage Calculator")
            print("Follow The On Screen Instructions To Find Your Final Earnings ")
        welcome()
        earnings = float(input("Enter Earnings Here: "))
    #This Is Where We Calculate The Deductions Of The Wages From Gross Pay with the function wage_calc
        def wage_calc():
            print("")
            print("Gross Pay: €", ("%.2f"%earnings))
            print("")
            print("Less Deductions")
            print("---------------")
            print("PRSI: €", ("%.2f"%(earnings*prsi)))
            print("Health Contribution: €", ("%.2f"%(earnings*health_contribution)))
            print("PAYE: €", ("%.2f"%(earnings*paye)))
            print("USC: €", ("%.2f"%(earnings*usc)))
            print("")
    #This Is Where We Call These Deductions Into The Programme
        wage_calc()
    #This Is Where The Final Wage Is Calculated & Displayed im the function final_wage
        def final_wage():
            print("Net Pay: €", ("%.2f"%(earnings-((earnings*prsi)+(earnings*health_contribution)+(earnings*paye)+(earnings*usc)))))
            print("Programme Completed")
        final_wage()
    #This Is Where We Call The Main Programme To Run
    main()
    

    Yeah sorry I didnt know how to do that.


  • Advertisement
  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Talisman wrote: »
    If you're not allowed to pass the parameter to the functions then earnings must be a global variable. In Python variables exist only in the first scope where they're assigned.

    Add the following line after the declaration of the other global variables:
    earnings = None
    

    Thats the thing we arent let use global variables. Global constants we were let use for PRSI etc. Its annoying, I can usually figure things out by playing with it. But putting functions into other functions when there is figures involved isnt proving difficult with the restraints applied


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭pillphil


    No worries, I was just posting it in case someone else wanted a look at it.

    I think you need to either pass a variable or use global variables. Not sure you can do it any other way.


    When you said doing it talisman's way didn't work, what did you try?

    In addition to this
    def main():
            welcome()
            earnings = float(input("Enter Earnings Here: "))
            wage_calc(earnings)
            final_wage(earnings)
    

    you would need to modify wage_calc() and final_wage() to accept a variable

    Post the code that didn't work so we can see what you have.
    The instructions from the lecturer would help too.


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Assignment 3.pdf

    So this is my assignment. Its super beginners. And we have been given strict instructions on only using what we have learned in class and one of his rules is only to use Global variables when he states it hence why I cant have earnings as one :(


  • Registered Users, Registered Users 2 Posts: 6,282 ✭✭✭Talisman


    You have to pass the earnings value to the functions. You say you haven't covered that in your course, but your code suggests that you have - you just don't realise it.

    You're using Python 3.x, print is a function and not a statement.
    print("Welcome To Your Wage Calculator")
    

    This is equivalent to:
    my_string = "Welcome To Your Wage Calculator"
    print(my_string)
    

    Instead of passing a variable to the print() function your code has passed a string value.


Advertisement