Loop
There are currently xxx tickets remaining.
How many tickets would you like to purchase?
(make sure you print an error message if they try to buy more than 4)
The total number of buyers was xxx.
python, keep it simple
def TicketSeller():
#initialize available_tickets to 20
available_tickets = 20
#count is used to keep track of no of customers
count = 0
flag = 1
#to continue run loop untill available_tickets becomes 0
while(flag == 1):
print("There are currently",available_tickets ,"tickets remaining")
#takes the no of tickets entered by user and store it in a tickets variable
tickets = int(input("How many tickets would you like to purchase? "))
#if user entered more than 4 tickets then prints the error message
if(tickets > 4):
print("You can buy maximum 4 tickets!!!")
#if available_tickets < tickets then prints message
if(available_tickets < tickets):
print("Only ",available_tickets,"tickets available !!!")
#if user entered less than or equal to 4 and
#available_tickets is greater than or equal to tickets
#than increment the count of customer
# reduce the available_tickets value
if(tickets <= 4 and available_tickets >= tickets ):
count = count + 1
available_tickets = available_tickets - tickets
#if available_tickets is 0 then set flag to 0 so while loop terminates
if(available_tickets <= 0):
flag = 0;
#returns the customer count
return count
#stores the count returned by the TicketSeller method in buyers variable
buyers = TicketSeller()
#print the result
print("The total number of buyers was: ",buyers)
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.