dimanche 29 avril 2018

Why does my "if" statement not exit out when I use the "break" function?

This code is like a ticket box counter, where the user selects if they want a ticket based on the price, or the seat. I haven't gotten to the seat selection part of the code, but when I was making my price selection one, after it's done, it prints out the layout too many times (instead of one, how it should be). After the "if" is triggered the first time, it should print out the layout, and then break. Though, it doesn't do this, and instead keeps on printing out the layout and going through the if function many times. Please help me fix this. Thank you!

line1 = [10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10]
line2 = [10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10]
line3 = [10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 , 10]
line4 = [10 , 10 , 20 , 20 , 20 , 20 , 20 , 20 , 10 , 10]
line5 = [10 , 10 , 20 , 20 , 20 , 20 , 20 , 20 , 10 , 10]
line6 = [10 , 10 , 20 , 20 , 20 , 20 , 20 , 20 , 10 , 10]
line7 = [20 , 20 , 30 , 30 , 40 , 40 , 30 , 30 , 20 , 20]
line8 = [20 , 30 , 30 , 40 , 50 , 50 , 40 , 30 , 30 , 20]
line9 = [30 , 40 , 50 , 50 , 50 , 50 , 50 , 50 , 40 , 30]
seats = [line1, line2 , line3 , line4 , line5 , line6 , line7 , line8 , line9]

for line in seats:
    print(line) 

seatFound = False

SorP = input("Would you like to select a seat based on the price (P) or seat (S)")

if SorP == "P":
    price = int(input("What price would you like?"))
    for line in seats:
        for seat in line:
        if seat == price:
            print("There is a seat available for that price")
            seatFound = True
            position = line.index(seat)
            line.remove(seat)
            line.insert(position , 0)
            for line in seats:
                print(line) 
            break
if seatFound != True:
    print("There is not a seat available for this price. Try again")

Aucun commentaire:

Enregistrer un commentaire