I'm trying to write a program that asks the user their hours and returns it to the main program, and then their rate of pay and returns it to the main program. I can't run the program because it is saying there is a problem with this part of my code towards the end, it's not liking the quotation mark for some reason. of" The program may have other problems but I don't know because I can only get this far.


It is supposed to have 3 functions in addition the main. when it is done it should display something to this effect.

Pay rate $10.00
Regular Hours 40
Overtime hours 20
Regular pay $400.00
Overtime pay $300.00
Total Pay $700.00

def ask_hours():
         return input ("How many hours did you work? ")
         rate = raw_input ("What is your rate of pay? ")

def findrate():
         pay = hours * rate
         overtime = (hours - 40) * (rate * 1.5)
         totalpay = pay + overtime
         print "You earned", pay, "regular pay and", overtime "overtime pay which is a total of", totalpay

def main() :
    hours = ask_hours()
    findrate()

main()

Recommended Answers

All 10 Replies

One problem is the return statement at the start of ask_hours().

def ask_hours():
         return input ("How many hours did you work? ") # Unconditional return from ask_hours()
         rate = raw_input ("What is your rate of pay? ")   # <-- this statement is not part of ask_hours()

You could make up a similar ask_rate() or change ask_hours() to return the pair (hours,rate) instead of just hours.

I will try and rework it when I get checked into my hotel. What was the problem python was seeing with the " mark though?

You need to study up on argument passing to and from functions. I put some comments into your code to help you:

def ask_hours():
    hours = float(raw_input ("How many hours did you work? "))
    rate = float(input ("What is your rate of pay? "))
    # return the two arguments as tuple
    return hours, rate

def findrate(hours, rate):
    if hours <= 40:
        pay = hours * rate
        overtime = 0.0
    if hours > 40:
        pay = 40 * rate
        overtime = (hours - 40) * (rate * 1.5)
    totalpay = pay + overtime
    print "You earned", pay, "regular pay and", overtime, \
        "overtime pay which is a total of", totalpay

def main() :
    # note that the function returns two arguments
    hours, rate = ask_hours()
    # pass the two arguments to this function
    findrate(hours, rate)

main()

It's not possible to tell without a line number for the error. I do see that there isn't a comma after overtime in this line
print "You earned", pay, "regular pay and", overtime "overtime pay which is a total of", totalpay

Alright, so I'm extremely close. I just can't get the Total Pay at the very end of the program to work correctly. If I put in 50 for hours and 10 for rate, I end up with 415.

def ask_hours():
          hours = float(raw_input ("How many hours did you work? "))
          rate = float(input ("What is your rate of pay? "))
          return hours, rate
     
def findpay(hours, rate):
         if hours <= 40:
             pay = hours * rate
         overtime = 1.5
         if hours > 40:
             pay = hours * rate
         overtime = (hours - 40) * (rate * 1.5)
         totalpay = pay + overtime
                       
def main() :
        # note that the function returns two arguments
        hours, rate = ask_hours()
        # pass the two arguments to this function
        findpay(hours, rate)
        print "Pay rate", rate
        if hours <= 40:
            print "Regular hours", hours
        elif hours >= 40:
            print "Regular hours", 40
        print "Overtime hours", (hours - 40)
        if hours <= 40:
            print "Regular pay", (hours * rate)
        elif hours > 40:
            print "Regular pay", (40 * rate)
        if hours < 40:
            print "Overtime Pay", 0
        elif hours > 40:
            print "Overtime Pay", (hours - 40) * (rate * 1.5)
        if hours <= 40:
            print "Total Pay", (rate * hours)
        elif hours > 40:
            print "Total Pay", (rate * 40) + (hours - 40) * 1.5

An indention problem

def findpay(hours, rate):
         if hours <= 40:
             pay = hours * rate
         overtime = 1.5
         if hours > 40:
             pay = hours * rate

         ##  the next line should be indented so it 
         ##   executes under "if hours > 40"
         overtime = (hours - 40) * (rate * 1.5)
         totalpay = pay + overtime

##   possibly would be simplier
def findpay(hours, rate):
         pay = hours * rate
         overtime = 0 
         if hours > 40:
              ## overtime=rate * 0.5 because the
              ## pay = hours * rate line above already
              ## paid for regular time
              overtime = (hours - 40) * (rate * 0.5)
         totalpay = pay + overtime
##   possibly would be simplier
def findpay(hours, rate):
         pay = hours * rate
         overtime = 0 
         if hours > 40:
              ## overtime=rate * 0.5 because the
              ## pay = hours * rate line above already
              ## paid for regular time
              overtime = (hours - 40) * (rate * 0.5)
         totalpay = pay + overtime

using .5 wouldn't work because if I get $10 an hour regular 10 * .5 = 5 so I would get $5 an hour overtime pay. I just have a problem at the end of the program.
Here, I mean if the hours are <=40 it works fine, but I have a problem figuring the overtime in the total pay.

if hours <= 40:
            print "Total Pay", (rate * hours)
        elif hours > 40:
            print "Total Pay", (rate * 40) + (hours - 40) * 1.5

It's 1.5 * rate

if hours <= 40:
            print "Total Pay", (rate * hours)
else:     ## can only be > 40
            print "Total Pay", (rate * 40) +((hours - 40) * 1.5*rate)

            ## or - if this is more understandable 
            ## the computer does one calc at a time anyway
            ot_rate = rate*1.5
            ot_hours = hours-40
            print "Total Pay", (rate*40) + (ot_rate * ot_hours)

Thanks, it works great. I'm surprised some of this is actually starting to sink in, slowly but surely. I was just stumped on getting it to calculate the total pay w/ the overtime hours w/ the order of operations.

Alright, so I still don't have 3 functions. I don't know how to set up the 2nd function.
1st function asks for hours and rate
2nd function calculates the regular hours and overtime hours and returns this information to main. <---This is where I'm lost, how would I set that up?
" You need a third function that does most of the work that you have in
main. If you do this and then have trouble send it back to me. I
have pasted part of the instructions below.

The third function calculates the regular pay, (regular hours times pay
rate); overtime pay, (overtime hours times times overtime pay rate) and
the total pay and returns this information to main. Main will then
display this information on the screen like the sample below. (Values
will have to be passed and returned)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.