1

I'm new to python and had a question about to to use more functions in a code besides def main():

My code below works, but I am trying to add new def's to their respective areas.

So like a new def called (def calcPay():), to where the hours enter are calculated (regPay, overtimePay, and total) as 3 separate items. & Also add a new def called (def displayOutput():), the function would receive all three of the values from (overtimePay, regPay, and totalPay) and print the message below.

If someone could explain to me how to use new functions besides main, that would be greatly appreciated.

Thanks, here is my code:

def main():

    try:
        hoursWorked = float(input("How many hours did you work? "))


        if hoursWorked > 40:
                overtimePay = (hoursWorked - 40) * 15
                regPay = 40 *10
                totalPay =( overtimePay + regPay)

        else:
            regPay = hoursWorked * 10
            overtimePay = 0
            totalPay = (regPay + overtimePay)


        print("You earned",'${:,.2f}'.format(regPay),"in regular pay",'${:,.2f}'.format(overtimePay),
              "in overtime for a total of",'${:,.2f}'.format(totalPay))
    except:
        print("Sorry, that wasn't a valid number. Ending program")


main()
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Sea W
  • 37
  • 1
  • 1
  • 5
  • 1
    There is nothing special about a function called main. Just define a function with the name you want, write its arguments and then the body of the function. Just like you did with `main()`. It's not clear to me what you're struggling with. – Reti43 Oct 11 '17 at 20:43
  • If you are new to Python, please [read the official tutorial](https://docs.python.org/3/tutorial/index.html) or check out [another tutorial](https://sopython.com/wiki/What_tutorial_should_I_read%3F). What you are asking is very basic language knowledge, making this question is far too broad. – poke Oct 11 '17 at 20:48

2 Answers2

0

You can declare your functions outside of the main function declaration and then use them in the main function (or inside of other functions in the main function).

So you could do something like:

def calcPay(hours):
    # Does logic
    return [400, 30, 430]

def displayOutput(regPay, overtimePay, totalPay):
    # Prints formatted string

def main():
    hoursWorked = float(input("How many hours did you work? "))
    pay = calcPay(hoursWorked)
    displayOutput(pay[0], pay[1], pay[2])

main()
BenWurth
  • 790
  • 1
  • 7
  • 23
-2

Have a look at these similar questions:

  1. What does it mean to call a function?

  2. How to correctly define a function?

  3. Basic explanation of Python functions?

There's nothing special about the function named main. You can name functions whatever you want.

When you "call" a function you're just jumping from one block of code to another. When the function returns it goes back to the line that called it.

def something():
    print('something')

def other():
    print('else')

def a_value():
    return 100

something()
other()
x = a_value()
print(x)

# ~~~~~ output
something
else
100

In your example a good use of a function would be to calculate the employee's pay.

def total_pay(hours_worked, hourly_rate, overtime_after=40):
    base_pay = min(overtime_after, hours_worked) * hourly_rate
    overtime_pay = max(0, hours_worked - overtime_after) * (hourly_rate * 1.5)
    return base_pay + overtime_pay

This function allows us to define the three things that determine a worker's pay. The base_pay will be, at most, the number of hours before overtime is applied. The overtime_pay will be from 0 to "some other limit" that's not defined here. Overtime is given time and a half (1.5).

blakev
  • 4,154
  • 2
  • 32
  • 52