# 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...
如何输入0打印"退出程序"然后退出?
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...')
答案 0 :(得分:0)
不要重复这样的代码。而是使用while True:
和条件break
作为适当的位置。这是标准的' loop-and-a-half' 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)
我会替换' 0'在提示中没有'没有'并进行测试if not name:
,但这是一个小问题。