Como interromper o ciclo para interromper a execução do programa

Oct 15 2020

Estou tentando implementar um programa em que desejo verificar os critérios de que, uma vez atingido o limite do número de mesas ou cadeiras, o loop deve ser interrompido e imprimir a declaração de impressão final. Logicamente, após a primeira instrução break, ela interrompe o primeiro loop for, mas não interrompe o loop for completo e imprime a instrução final e continua iterando.

Eu quero que o número máximo de mesas ou cadeiras seja atingido, o loop deve quebrar.

Alguma ideia de como modificar? Posso implementar a instrução while, em caso afirmativo - como?

Eu sou novo em python e acho esses exercícios interessantes e sua ajuda será ótima.

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

Respostas

2 ForceBru Oct 14 2020 at 22:38

Você pode adicionar outra variável para controlar o fato de que o loop interno foi interrompido:

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

Talvez o mais fácil seja apenas envolvê-lo em uma função:

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

Eu faria assim:

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