jeudi 28 février 2019

Recurring line disappearance, issue with while loop

*I'm new to python, so be gentle...

Overall issue: I have had an issue in multiple codes I've written lately where lines get skipped and it is obviously an error on my part. I know I must be messing up ordering of something, but I'm just not seeing it. My most recent issue can be found here: Is there any reason Python would skip a line?

Now, I want to write an application to pre-sell a limited number of tickets. Here are the conditions:

"Each buyer can buy as many as 4 tickets. There are a total of 15 tickets that are available to pre-sell. The program should prompt the user for the amount of tickets they want to purchase and then display the number of remaining tickets. Repeat until all tickets have been sold, and then display the total number of buyers."

A similar problem is occurring.

buy = int()
ticket_num = 15
buyers = 0

while ticket_num > 0:
    buy = int(input("How many tickets would you like to purchase? "))
    if buy > 4:
        print("You cannot buy that many (4 max).")
        buy = input("How many tickets would you like to purchase? ")
    else:
        ticket_num = ticket_num - buy
        print("There are currently", ticket_num, "remaining.")
        buyers = buyers + 1

print() 

print("The total number of buyers was:", buyers)

It appears that the print line in the 'else' structure is not being read and I don't quite understand why...

Can anyone lend me any insight into what my overall misunderstanding is..?

Aucun commentaire:

Enregistrer un commentaire