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

Question:

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

Asked By: Toni Rana

||

Answers:

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.

Answered By: Bilesh Ganguly

If you want to update the value of count, the easiest and clearest method is to update it on the line before the print statement. Alternatively, you could update in the other part of the code where you update the value of sold.

if sold == 0:  # If tickets are equal to 0
        print("Welcome to Movies4Us!")  # Prints "Welcome to Movies4Us!" if condition above is satisfied
    else:
        count += 1
        print(f"You are buyer NO.{count}.", end=" ") # Use string interpolation
        print(f"The number of remaining tickets is now {remaining}.")  # Prints user's number and remaining amount of tickets

I also updated this to use f-strings for formatting, otherwise you will have the problem of there being an extra space between the value of count and the following ".".

If you really want to not have a separate line for count+=1, it is possible to update count inline in the print statement, but only if you don’t use f-strings, and if you do use the := operator, because the arguments to the print statement must be expressions, so a normal assignment would be a syntax error:

print('You are buyer NO.', count := count +1, ". The number of remaining tickets is now", remaining, ".")  # Prints user's number and remaining amount of tickets
Answered By: nigh_anxiety
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.