-2
# ABC Inc., Gross Pay Calculator!
# Enter employee's name or 0 to quit : Nathan
# Enter hours worked : 35
# Enter employee's pay rate : 10.00
# Employee Name : Nathan
# Gross Pay: 350.0
# Enter next employee's name or 0 to quit : Toby
# Enter hours worked : 45
# Enter employee's pay rate : 10
# Employee Name : Toby
# Gross Pay : 475.0
# (overtime pay : 75.0 )
# Enter next employee's name or 0 to quit : 0
# Exiting program...

How do i make the input for 0 print "exiting program" then exit?

print('ABC inc., Gross Pay Calculator!')
name = input("Enter employee's name or 0 to quit:")
hours = float(input("Enter hours worked:"))
payrate = float(input("Enter employee's pay rate:"))
print("Employee Name:", name)
grosspay = hours * payrate
print("Gross pay:", grosspay)
if hours > 40:
print("(Overtime pay:", (hours - 40) * payrate * 1.5)
while name!=0:
    name = input("Enter next employee's name or 0 to quit:")
    hours = float(input("Enter hours worked:"))
    payrate = float(input("Enter employee's pay rate:"))
    print("Employee Name:", name)
    grosspay = hours * payrate
    print("Gross pay:", grosspay)
    if hours > 40:
        print("(Overtime pay:", (hours - 40) * payrate*1.5)
else:
    print ('Exiting program...')
Kalpesh Dusane
  • 1,477
  • 3
  • 20
  • 27

1 Answers1

0

Do not repeat code like that. Instead use while True: and conditional break as the appropriate place. This is the standard 'loop-and-a-half' idiom in Python.

print('ABC inc., Gross Pay Calculator!')
while True:
    name = input("Enter employee's name or 0 to quit:")
    if name == '0':
        print ('Exiting program...')
        break
    hours = float(input("Enter hours worked:"))
    payrate = float(input("Enter employee's pay rate:"))
    print("Employee Name:", name)
    grosspay = hours * payrate
    print("Gross pay:", grosspay)
    if hours > 40:
        print("(Overtime pay:", (hours - 40) * payrate * 1.5)

I would replace '0' in the prompt with 'nothing' and make the test if not name:, but this is a minor issue.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52