支付计算器中的意外循环
Unexpected loop in payment calculator
我有 运行 这段代码,我更改它的次数多得我数不过来,但它总是会导致循环。我不太确定我做错了什么,也不知道如何结束循环。
"""
WeeklyPay.py:为所有按小时计酬的员工生成工资单存根并进行汇总
"""
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()
您正在使用 while
循环,其中变量 employee
从未更改,因此条件保持 True
。如果您将 while
替换为 if
.
,它应该可以工作
您可能会发现下面显示的 while
循环更像是您的程序应该执行的操作:
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()
我有 运行 这段代码,我更改它的次数多得我数不过来,但它总是会导致循环。我不太确定我做错了什么,也不知道如何结束循环。 """ WeeklyPay.py:为所有按小时计酬的员工生成工资单存根并进行汇总 """
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()
您正在使用 while
循环,其中变量 employee
从未更改,因此条件保持 True
。如果您将 while
替换为 if
.
您可能会发现下面显示的 while
循环更像是您的程序应该执行的操作:
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()