ThatOneGuy
ThatOneGuy

Reputation: 37

Assistance/Review of my python 3 for a fictitious bus seating problem

Current script as standing

totSeat = 100

print("Seats Remaining", totSeat, end = " ")
gSeat = int(input("How many in your group?"))
while gSeat != 100:
    totSeat -= gSeat
    if gSeat <= 100:
        print("Booked, Thank You")
        print("Seats Remaining", totSeat, end=" ")
        gSeat = int(input("How many in your group?"))
        if totSeat <= 1:
            print("Booked, Thank You\nSOLD OUT")
            break
    if gSeat < totSeat:
        print("Sorry, not enough seats left.")
        gSeat = int(input("How many in your group?"))

Basically

if 'totSeat' (total seats) is 0 i need it to break and say sold out. if 'gSeat' (guest seat) is above the 'totSeat' (including if use inputs 101+) i need to to display "Sorry, not enough seats."

As the scipt stands it kinda does this but gets a bit fucky and even dips into -1 seats available before breaking. I am extremely new to python as apologies for really rough scripting

Upvotes: 4

Views: 142

Answers (2)

U13-Forward
U13-Forward

Reputation: 71610

Try changing your code to:

totSeat = 100

print("Seats Remaining", totSeat)
gSeat = int(input("How many in your group?"))
while gSeat != 100:
    if gSeat <= totSeat:
        print("Booked, Thank You")
        totSeat -= gSeat
        if totSeat == 0:
            print('SOLD OUT')
            break
        print("Seats Remaining", totSeat, end=" ")
        gSeat = int(input("How many in your group?"))
    if gSeat > totSeat:
        print("Sorry, not enough seats left.")
        gSeat = int(input("How many in your group?"))

Example output:

Seats Remaining 100
How many in your group?56
Booked, Thank You
Seats Remaining 44 How many in your group?43
Booked, Thank You
Seats Remaining 1 How many in your group?34
Sorry, not enough seats left.
How many in your group?1
Booked, Thank You
SOLD OUT

Upvotes: 4

Yash Patel
Yash Patel

Reputation: 125

Here is the script you may need,

totSeat = 100

print("Seats Remaining", totSeat, end = " ")

while totSeat > 0:
    gSeat = int(input("How many in your group?"))
    if gSeat > totSeat:
        print("Sorry, not enough seats left.")
    else:
        totSeat-=gSeat
        print("Booked, Thank You")
        print("Seats Remaining", totSeat, end=" ")
print("Sold out")

Upvotes: 5

Related Questions