【问题标题】:Unexpected loop in payment calculator付款计算器中的意外循环
【发布时间】:2020-01-20 14:35:48
【问题描述】:

我已运行此代码并对其进行了多次更改,但它总是导致循环。我不太确定我做错了什么,也不知道如何结束循环。 """ 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()

【问题讨论】:

    标签: python-3.x loops calculator


    【解决方案1】:

    您可能会发现下面显示的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()
    

    【讨论】:

      【解决方案2】:

      您正在使用while 循环,其中变量employee 永远不会更改,因此条件保持True。如果您将while 替换为if,它应该可以工作。

      【讨论】:

      • 虽然这回答了 OP 的问题,但它对他们没有多大帮助,因为 if 子句将始终执行,因为 employee == "Y" or "y" 将始终导致 True
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多