Reputation: 37
seatList1 = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10 }
I made the above dictionary as a airline seat list. Once users choose an empty seat and enter their name and other details in, the value of seat they choose will become their names, for example:
Before they choose:
1: 1
After:
1: Tom
The following is my identify program. 'Detail' is a string which included "Passenger's" details.
while (True):
seatNum = int(input("Enter the seat number:"))
if isinstance(seatList1[seatNum], str):
print("This seat has been ordered, please enter another one.")
else:
print("Success")
seatList1[seatNum] = detail
print(seatList1)
break
So that this program can identify a seat is empty or not by its value is a string or integer. But the problem is, I want this program can print the empty seat at the first time, just like this(pretend number 1 seat has been taken):
The available seats are 2, 3, 4, 5, 6, 7, 8, 9, 10
So that "passengers" can know which seats are available for them to choose. Is there a way for me to solve this problem without writing more list? Thank you very much!
Upvotes: 1
Views: 111
Reputation: 1064
How do you like the idea of using a set for empty seats, and a dictionary for taken seats? That way, you don't have to check the type of the key returned for a seat, which is not always a robust choice in a duck-typing language such as Python.
max_seat_number = 10
empty_seats = set(range(1, max_seat_number + 1))
taken_seats = {}
print("Empty seats : ", empty_seats)
while True:
name = input("Enter the passenger name (or type 'quit'): ")
if name == "quit":
break
while True:
seat_requested = int(input("Enter the seat number : "))
try:
empty_seats.remove(seat_requested)
taken_seats[seat_requested] = name
break
except KeyError:
print("Seat", seat_requested, "is not available.")
print("Empty seats : ", empty_seats)
print("Taken seats :", taken_seats)
print()
Here's some sample output. More error checking and prompting could be added.
Empty seats : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Enter the passenger name (or type 'quit'): John
Enter the seat number : 5
Taken seats : {5: 'John'}
Enter the passenger name (or type 'quit'): George
Enter the seat number : 8
Taken seats : {5: 'John', 8: 'George'}
Enter the passenger name (or type 'quit'): Mark
Enter the seat number : 5
Seat 5 is not available.
Empty seats : {1, 2, 3, 4, 6, 7, 9, 10}
Enter the seat number : 2
Taken seats : {5: 'John', 8: 'George', 2: 'Mark'}
Enter the passenger name (or type 'quit'): Mary
Enter the seat number : 9
Taken seats : {5: 'John', 8: 'George', 2: 'Mark', 9: 'Mary'}
Enter the passenger name (or type 'quit'): quit
Upvotes: 2
Reputation: 2702
I don't want to know how long I spent on this, I might have gotten slightly carried away.
from typing import NamedTuple, Optional
class Seat(NamedTuple):
available: bool = True
data: Optional[str] = None
seats = dict.fromkeys((str(elem) for elem in range(1, 11)), Seat())
print(seats)
while True:
print(f"\nCurrent seat data:\n{'seat number':<11} {'available':<10} {'data':<20}")
for seat_num, seat_obj in seats.items():
print(f'{seat_num!s:<11} {seat_obj.available!s:<10} {seat_obj.data!s:<20}')
seat_num_in = input('Enter the seat number: ')
seats_dict_res = seats.get(seat_num_in)
if not seats_dict_res:
input('\nERROR: The seat number is invalid.\nPress ENTER to continue.')
elif not seats_dict_res.available:
input('\nERROR: That seat is not available.\nPress ENTER to continue.')
else:
passenger_name_in = input("Enter the passenger's name: ")
seats[seat_num_in] = Seat(available=False, data=passenger_name_in)
This solution makes use of typing.NamedTuple
, which are adapted from the standard collections.namedtuple()
. The formatting is done with the new (ish) f-strings.
Upvotes: 1
Reputation: 559
Something like this?
print ([i for i in seatList1.keys() if isinstance(seatList1[i], int)])
Upvotes: 0
Reputation: 3226
You can use the same logic as your code above. If User already booked that seat, then value for that seat number in seatList1
will be string. You can just do -
for k, v in seatList1.items():
if not isinstance(v, str):
print (k)
If you want to print the available seats as a comma seperated string, you can use join()
-
",".join(str(k) for k, v in seatList1.items() if not isinstance(v, str))
Upvotes: 1