Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a counter in python every time name is written it goes up by 1

I would like the Buyer Number to change every time there is a new buyer

total = 200  # Assigns 200 tickets to variable total
sold = 0  # Assigns 0 tickets to variable sold
count = 0
while sold < total:  # Runs while loop as long as tickets sold is less than total tickets
    remaining = total - sold  # Assigns total - sold to variable remaining
    if sold == 0:  # If tickets are equal to 0
        print("Welcome to Movies4Us!")  # Prints "Welcome to Movies4Us!" if condition above is satisfied
    else:
        print('You are buyer NO.', count + 1, "." "The number of remaining tickets is now", remaining, ".")  # Prints user's number and remaining amount of tickets
    name = input('What is your name?: ')
    tickets = input("How many tickets would you like to purchase?: ")  # Asks user to enter desired amount of tickets to purchase
    try:
        tickets = int(tickets)  # Converts variable tickets to integer type
        if tickets > remaining:  # If user enters more tickets than remaining tickets available
            print("Sorry, there aren't that many tickets available.")  # Prints no more tickets available
        else:
            sold += tickets  # Updates value of variable sold into tickets
            print("You have purchased", tickets, "tickets, Enjoy the movie", name, '!')  # Prints the amount of tickets the user purchased
    except ValueError:  # If user inputs anything except for an integer
        print("Invalid input. Please enter a number.")  # Prints invalid input and asks user to input again
        continue  # Ends current loop and runs the while loop again

print ("There are no more tickets available.")  # If number of tickets reached 0, program ends

Tried adding count = 0 and count + 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’ve already initialized a counter: count = 0. You just need to increment it inside the loop whenever a valid ticket purchase is made.

Add count += 1 inside the else block where a valid ticket purchase is made under the statement sold += tickets. This will increment the counter whenever a successful purchase happens.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading