Comment rompre une boucle pour arrêter l'exécution du programme

Oct 15 2020

J'essaie de mettre en œuvre un programme où je veux vérifier par rapport aux critères qu'une fois la limite du nombre de tables ou de chaises atteinte, la boucle doit se rompre et imprimer le relevé final d'impression. Logiquement après la première instruction break, elle s'interrompt avec la première boucle for, mais elle ne rompt pas la boucle for complète et imprime l'instruction finale et continue à itérer.

Je veux que ceux que le nombre maximum de tables ou de chaises soit atteint, la boucle doit se rompre.

Une idée comment modifier? Puis-je implémenter l'instruction while, si oui - comment?

Je suis nouveau en python et je trouve ces exercices intéressants et votre aide sera formidable.

#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.")

Réponses

2 ForceBru Oct 14 2020 at 22:38

Vous pouvez ajouter une autre variable pour garder une trace du fait que la boucle interne a été interrompue:

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;
1 ChristianSloper Oct 14 2020 at 22:39

Le plus simple est peut-être de l'envelopper dans une fonction:

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.")
1 dzang Oct 14 2020 at 22:40

Je le ferais comme ceci:

#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.")