# Subtracting from a deffined variable that has an intager value in a loop

Hello, I am currently working on a programming assignment for a class I’ve gotten most of the program finished.

the problem I’m having is I can’t figure out how to get the program to subtract from the set number of seats and save that as the remaining number of seats in that section of the theater. thus when the program loops back around that is the number of seats that are still available.

this is my program so far:

def main():
Hall = 300
Mezz = 100
AHT = 10
CHT = 7
AMT = 8
CMT = 5
while True:
Place = int(input("Enter 1 for hall 2 for mezz "))
if (Place) <1 or (Place) >2:
print(“invalid input please press 1 or 2”)
else:
break
if Place == 1:
Children = input(“how many children? “)
print(“Great, you have " + Adults + " adults and " + Children + " children.”)
T2 = int(Children) * int(CHT)
HSL = Hall - TP
MSL = Mezz
FT = T1 + T2
print(“that will be \$” + str(T1) + " for the adults and \$” + str(T2) + " for the children.” )
print(“Your total is \$” + str(FT))
print(“remining seats: Hall:” + str(HSL) + " Mezz:” + str(MSL) )

``````    Restart = input("whould you like to return to start? type yes then enter or simply press enter to exit the program. ").lower()
if Restart == "yes":
main()
else:
print("goodbye")
exit()

elif Place == 2:
Children = input("how many children? ")
print("Great, you have " + Adults + " adults and " + Children + " children.")
T2 = int(Children) * int(CMT)
HSL = Hall
MSL = Mezz - TP
FT = T1 + T2
print("that will be \$" + str(T1) + " for the adults and \$" + str(T2) + " for the children.")
print("Your total is \$" + str(FT))
print("remining seats: Hall:" + str(HSL) + " Mezz:" + str(MSL))

Restart = input("whould you like to return to start? type yes then enter or simply press enter to exit the program. ").lower()
if Restart == "yes":
main()
else:
print("goodbye")
exit()
``````

main()

all replies and suggestions are welcome thank you.
also, forgive any grammar in the program it is rough because I am still working on the process and haven’t tuned grammar yet

It’s not helping that you’re using recursion when the user wants to return to the start and you’re not passing the number of (remaining) seats in.

It would’ve been simpler to ask in a loop, prompting with 1 for hall, 2 for mezzanine, 0 for finish (break out of loop).

Lots of issues with your code, but if this is for a class you are still new to this, so that isn’t a surprise (or a problem). My biggest problem is “what is your question?” I don’t know.

One thing I would say about your code: Do all the prompting for number of adults and children (and confirming the read numbers) before you write the `if place == 1:`

I would also write a function called `input_int` which takes a prompt and returns the integer value of `input`

my apologies I am new to python and I just started this class a day ago. The program is supposed to represent a ticket sales system at a theater. If a customer comes in the program wants to know if they want to sit in the hall or the mezzanine. let’s say 2 adults and 4 children come in that’s 6 people total. if they all sit together in the hall that is 300 - 6 = 294. That means there are now only 294 seats left in the hall. What I’m asking is how do I get the program to carry the number of remaining seats back to the beginning of the program so that when another sale happens there are only 294 seats left instead of 300? This same concept also applies to the Mezzanine with the exception that that section only has 100 seats.

I’m not too sure if you’ve covered all of what I’ve to offer, but you may find this to be of help in any case. It’s not a full solution, as that would be handing it to you on a plate and you’d not learn too much, but it’s a start:

``````def mezzanine():
child = 5
seating = 100

def hall():
child = 7
seating = 300
print()
booked = bookings['hall']
if booked < seating:
print(f"We have {seating-booked} seats available in the Hall.")
print(f"Children: \$ {child:.2f}")

children = input("Number of children: ")

if total_seats <= seating - booked:
booked += total_seats
bookings['hall'] = booked
print()
print(f"You have booked {total_seats} seats.")

c_charge = int(children) * child
total_charge = a_charge + c_charge

print()
print(f"Children: {int(children)}. Charge: \${c_charge:.2f}")
print(f"Total to charge: \${total_charge:.2f}")
print()
else:
print(f"We don't have {total_seats} seats available in the Hall.\n")
else:
print("Sold out.\n")

bookings = {'hall': 0,
'mezzanine': 0
}

while True:
h, m = '1', '2'
area = input("Pleased select Hall(1) or Mezzanine(2): ")
if area not in (h, m):
print("Not valid.\n")
else:
if area == '1':
hall()
else:
mezzanine()
``````

In fact, the way this is panning out, there would be quite a lot of code repetition, which is never a good thing, so this could be improved upon.

… a little more…

If you use this as the main driver:

``````while True:
select = {'1': 'hall',
'2': 'mezzanine'
}
keys = select.keys()
area = input("Pleased select Hall(1) or Mezzanine(2): ")
if area not in keys:
print("Not valid.\n")
else:
book(select[area])
``````

… you can have just the one function: `def book(area):` which will work for both, with just some minor code changes. This is more than I intended to give you, but it’s here now, so I hope you can learn from it.

Thank you Rob after viewing your post I have found the answer I was looking for in the sample codes you posted. It was the part that had to do with book - booking. I am very new to the python language only ever having used java once before in a previous class. While it is a bit similar it’s not the same and therefore I have had difficulty writing this assignment. After seeing how to write in python properly I am going to rewrite the program from scratch and hopefully, it will perform as intended. I will update when finished. Again thank you for all the input.

You’re welcome.

Looking at what you first posted, it seemed to me that you have the basics nailed and just needed some guidance, which is not easy to provide using natural language, which is why I thought that a script would be much more informative.

Happy coding.

Gotcha. The simplest way to do this is to use the `global` statement. So something like:

``````mezzanine_seats = 100
def book_mezzanine(adults: int, children: int) -> None:
"""Book seats on the mezzanine"""
global mezzanine_seats
If you don’t use the `global` statement, you will get a local variable inside `book_mezzanine` which will be lost as soon as the function returns.
Use of the `global` statement is bad style. My preference would be to define a Location class which you construct with spaces and prices, and then a `price` function which returns the total price for a given number of adults and children (or `None` for “no room”), and a “book” function which reduces the available seating. Then you define a “hall” object and a “mezzanine” object, choose which object to refer to depending on “place”, and call the functions on your object. However that may be a little bit too much for someone coming very new to python