Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 719
ThatOneGuy Avatar asked Dec 31 '22 13:12

ThatOneGuy


2 Answers

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")
like image 100
Yash Patel Avatar answered Feb 06 '23 13:02

Yash Patel


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
like image 28
U12-Forward Avatar answered Feb 06 '23 11:02

U12-Forward