【问题标题】:How to break from loop to stop program from running如何从循环中中断以停止程序运行
【发布时间】:2020-10-14 15:34:42
【问题描述】:

我正在尝试实现一个程序,我想在其中检查一旦达到桌椅数量限制的标准,循环应该中断并打印最终的打印语句。从逻辑上讲,在第一个 break 语句之后,它会从第一个 for 循环中中断,但不会中断完整的 for 循环并打印最终语句并继续迭代。

我希望那些桌子或椅子达到最大数量,循环应该中断。

知道如何修改吗?如果可以,我可以实现 while 语句吗?如何实现?

我是 python 新手,发现这些练习很有趣,你的帮助会很大。

#variables
number_of_tables = 200
no_of_chairs = 500
for tables in range(0, number_of_tables):
 for chairs in range(0, no_of_chairs):
   prompt = "Bienvenue a la'restaurant"
   answer = input("Please enter for how many people you need a table: ")

   print(f"Welcome family of {answer}, please wait while we take you to your seat")
   number_of_tables -= 1
   no_of_chairs -= int(answer)

   if number_of_tables == 0 or no_of_chairs == 0:
     reply = "Good things need patience. We are full. Please wait in the lobby till a table gets empty. We thank you for understanding"
     print(reply)
     break;

print("Thank you for visiting us.")

【问题讨论】:

    标签: python python-3.x for-loop


    【解决方案1】:

    您可以添加另一个变量来跟踪内部循环中断的事实:

    number_of_tables = 200
    no_of_chairs = 500
    broken = False # this one
    
    for tables in range(0, number_of_tables):
     if broken:
       break # out of the outer loop
    
     for chairs in range(0, no_of_chairs):
       prompt = "Bienvenue a la'restaurant"
       answer = input("Please enter for how many people you need a table: ")
    
       print(f"Welcome family of {answer}, please wait while we take you to your seat")
       number_of_tables -= 1
       no_of_chairs -= int(answer)
    
       if number_of_tables == 0 or no_of_chairs == 0:
         reply = "Good things need patience. We are full. Please wait in the lobby till a table gets empty. We thank you for understanding"
         print(reply)
         broken = True # signal to the outer loop
         break;
    

    【讨论】:

      【解决方案2】:

      也许最简单的方法就是将它包装在一个函数中:

      def waiting(number_of_tables, no_of_chairs):
          for tables in range(0, number_of_tables):
           for chairs in range(0, no_of_chairs):
             prompt = "Bienvenue a la'restaurant"
             answer = input("Please enter for how many people you need a table: ")
          
             print(f"Welcome family of {answer}, please wait while we take you to your seat")
             number_of_tables -= 1
             no_of_chairs -= int(answer)
          
             if number_of_tables == 0 or no_of_chairs == 0:
               reply = "Good things need patience. We are full. Please wait in the lobby till a table gets empty. We thank you for understanding"
               print(reply)
               return
          
          
      
      waiting(200,500)
      print("Thank you for visiting us.")
      

      【讨论】:

        【解决方案3】:

        我会这样做:

        #variables
        number_of_tables = 200
        no_of_chairs = 500
        
        while number_of_tables != 0 and no_of_chairs != 0:
           prompt = "Bienvenue a la'restaurant"
           answer = input("Please enter for how many people you need a table: ")
        
           print(f"Welcome family of {answer}, please wait while we take you to your seat")
           number_of_tables -= 1
           no_of_chairs -= int(answer)
        
        reply = "Good things need patience. We are full. Please wait in the lobby till a table gets empty. We thank you for understanding"
        
        print(reply)
        print("Thank you for visiting us.")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-01
          • 1970-01-01
          • 2015-05-17
          • 2018-11-19
          • 2020-06-16
          • 1970-01-01
          • 2016-07-26
          • 2023-01-13
          相关资源
          最近更新 更多