0

I have a bit of code that calculates pay / overtime pay based on the hours worked in a week. There are two if statements that calculate the pay for week1 and week2. What I am trying to do is then calculate the total pay which is the pay for the results of the week1 if statement plus the results of the week2 if statement, but I'm struggling. I'm probably making this much more difficult than it needs to be.

I'm using a Jupyter Notebook where each of the chunks below are in a separate cell. The results of the first if statement = 440 the second if statement = 473. The desired result is to combine these so that the output is 913.

Any help or suggestions are greatly appreciated!

rate = 11
week1 = 40 
week2 = 42

if week1 <= 40:
    print((rate * week1))
else:
    print((week1 - 40)*(rate * 1.5) + (40 * rate))

if week2 <= 40:
    print(rate * week2)
else:
    print((week2 - 40)*(rate * 1.5) + (40 * rate))
bbalch
  • 33
  • 1
  • 2
  • 8
  • store your calculation results in variables instead of merely printing them. – Alex Hall Aug 30 '17 at 16:16
  • Make those identical if/else blocks functions that return the result, instead of just printing it. Then it should be trivial, or else I don't see what's the problem. – tobias_k Aug 30 '17 at 16:16
  • Thank you all for your help. I used the max / min option listed below and it worked perfectly. I like the ternary operator, in fact, that might be easier to dissect. – bbalch Aug 30 '17 at 21:28

4 Answers4

0

You can use a slightly more advanced feature called the ternary operator to do this:

rate = 11
week1 = 40 
week2 = 42

week1_pay = rate * week1 if week1 <=40 else (week1 - 40)*(rate * 1.5) + (40 * rate))
week2_pay = rate * week2 if week2 <=40 else (week2 - 40)*(rate * 1.5) + (40 * rate))

print(week1_pay)
print(week2_pay)
print(week1_pay + week2_pay)

This operator works very much like how it sounds when you read the code out loud. That is: variable x is equal to some value if some condition is true, else variable x is equal to some other value.

In a more simple example we can use:

number = 45
pos_or_neg = "negative" if number < 0 else "positive"

In this example pos_or_neg would evaluate to "positive" because 45 >= 0.

Ternary Help:

Does Python have a ternary conditional operator? https://docs.python.org/3.3/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator

Community
  • 1
  • 1
bendl
  • 1,583
  • 1
  • 18
  • 41
0
if week1 <= 40:
    #print((rate * week1))  # Not necessary, unless you need to print separate weekly pays as well.
    pay = rate * week1
else:
    #print((week1 - 40)*(rate * 1.5) + (40 * rate))
    pay = (week1 - 40)*(rate * 1.5) + (40 * rate)

if week2 <= 40:
    #print(rate * week2)
    pay += rate * week2
else:
    #print((week2 - 40)*(rate * 1.5) + (40 * rate))
    pay = (week2 - 40)*(rate * 1.5) + (40 * rate)
print(pay)
Siddardha
  • 510
  • 1
  • 7
  • 17
0
rate = 11;
week1 = 40;
week2 = 42;

if week1 <= 40 and week2 <= 40:
    print((rate * week1)+(rate * week2))
else:
    print("For Week1: ", (week1 - 40) * (rate * 1.5) + (40 * rate))
    print("For Week2: ", (week2 - 40) * (rate * 1.5) + (40 * rate))
    print("Total is: ", ((week1 - 40) * (rate * 1.5) + (40 * rate) + (week2 - 40) * (rate * 1.5) + (40 * rate)))

You can use variables too.

Bussller
  • 1,961
  • 6
  • 36
  • 50
0

After writing my initial answer, I though of a completely different one I think is better. You can use the max and min functions to make sure you don't add negative overtime.

rate = 11
week1 = 40 
week2 = 42

week1_pay = max(week1 - 40, 0)*(rate * 1.5) + (min(week1, 40) * rate)
week2_pay = max(week2 - 40, 0)*(rate * 1.5) + (min(week2, 40) * rate)

print(week1_pay)
print(week2_pay)
print(week1_pay + week2_pay)

This works by finding whichever is larger: weekX - 40 or 0 and multiplying it by rate * 1.5. This will be zero if there was fewer than 40 hours. It then finds the smaller of weekX and 40 and multiplies that by rate to find the standard pay and adds it to the overtime pay.

Min and max docs:

https://docs.python.org/3/library/functions.html#max https://docs.python.org/3/library/functions.html#min

Community
  • 1
  • 1
bendl
  • 1,583
  • 1
  • 18
  • 41