1

I am trying to implement a program where I want to check against the criteria that once the limit of number of tables or chairs is reached, the loop should break and print the final print statement. Logically after the first break statement, it breaks from the first for loop but it does not break the complete for loop and print the final statement and keeps iterating.

I want that ones the max number of tables or chairs is reached, the loop should break.

Any idea how to modify? Can I implement while statement, if so - how?

I am new to python and find these exercises interesting and your help will be great.

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

3 Answers 3

2

You can add another variable to keep track of the fact that the inner loop was broken out of:

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;
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe easiest is just to wrap it in a function:

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

Comments

1

I would do it like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.