1

I have the following code:
For some reason, the program ignores the 2nd 'if' statement. Does anyone have any idea why, please?

#define function

def CalculateBasicPay (hours, rate):
     pay = hours * rate
     return pay

def CalculateOvertimePay (overtime_hours, overtime_rate):
     overtime = overtime_hours * overtime_rate * 1.5
     return overtime

#main program to get user input
hoursWorked = int()

if hoursWorked < 40:
    converted_hours = float(input("Enter number of hours: "))
    converted_rate = float(input("Enter your rate: "))
    totalHours = CalculateBasicPay(converted_hours,converted_rate)
    print("Your total pay is: £", totalHours)
    
if hoursWorked > 40:
    converted_hours = float(input("Enter number of hours: "))
    converted_rate = float(input("Enter your rate: "))
    totalHours2 = CalculateOvertimePay(converted_hours,converted_rate)
    print("Your total pay is: £", totalHours2)

----------

The output is only taking the 1st condition always:

Enter number of hours: 5
Enter your rate: 2
Your total pay is: £ 10.0
>>> 

Enter number of hours: 50
Enter your rate: 2
Your total pay is: £ 100.0


-----------

I'm brand-new to python! So please be nice :)

Cheers :)

2
  • 1
    in addition to the other flaws, you should consider what happens if hoursWorked == 40 Commented Aug 13, 2020 at 10:38
  • If you are using int() to initialise hoursWorked, as seems to be the case from what is posted here, its value will be 0, which will always be less than 50 Commented Aug 13, 2020 at 18:03

2 Answers 2

1

You should get the hours worked outside the if statement:

#define function

def CalculateBasicPay (hours, rate):
     pay = hours * rate
     return pay

def CalculateOvertimePay (overtime_hours, overtime_rate):
     overtime = overtime_hours * overtime_rate * 1.5
     return overtime

#main program to get user input
hoursWorked = float(input("Enter number of hours: "))
converted_rate = float(input("Enter your rate: "))

if hoursWorked < 40:    
    totalHours = CalculateBasicPay(converted_hours,converted_rate)
    print("Your total pay is: £", totalHours)
    
if hoursWorked > 40:
    totalHours2 = CalculateOvertimePay(converted_hours,converted_rate)
    print("Your total pay is: £", totalHours2)
Sign up to request clarification or add additional context in comments.

Comments

1

Your line hoursWorked = int() doesn't get an input from the user, it just creates an integer with the value 0.

You should replace it with something like:

hoursWorked = int(input("How many hours have you worked: "))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.