Inge Teleky
Inge Teleky

Reputation: 39

Unexpected loop in payment calculator

I have run this code and changed it more times than I can count but it always results in a loop. I'm not quite sure what I've done wrong nor how to end the loop. """ WeeklyPay.py: generate payroll stubs for all hourly paid employees and summarizes them """

 def main():
    """
total_gross_pay = 0
hours_worked = 0
gross_pay = 0
hourly_rate= 0

:return: None
"""



 employee = input("Did the employee work this week? Y or y for yes: ")
 while employee == "Y" or "y":
    hours_worked = int(input("How many hours did the employee work this week? "))
    hourly_rate = int(input("What is the employee's hourly rate? "))



 gross_pay = hours_worked * hourly_rate


 print("Your weekly pay is: "+ gross_pay)



 main()

Upvotes: 1

Views: 39

Answers (2)

Noctis Skytower
Noctis Skytower

Reputation: 22011

You might find the while loop shown below works more like what your program should do:

def main():
    """Help the user calculate the weekly pay of an employee."""
    while input('Did the employee work this week? ') in {'Y', 'y'}:
        hours_worked = int(input('How many hours? '))
        hourly_rate = int(input('At what hourly rate? '))
        gross_pay = hours_worked * hourly_rate
        print('The weekly pay is:', gross_pay)


if __name__ == '__main__':
    main()

Upvotes: 1

Bart Barnard
Bart Barnard

Reputation: 1168

You are using a while loop, in which the variable employee is never changed, so the condition stays True. It should work if you replace the while with an if.

Upvotes: 0

Related Questions