The following code is used by an airline company for their passenger management system. The code asks the user to select among the following options and then performs the required task:
- add
- del
- find
- exit
add: Asks the user to enter a name and destination (from a list), then adds that customer to a passenger list (i.e. passengers)
del: Asks the user to enter a name and deletes the first occurrence of the customer to a passenger list (i.e. passengers)
find: Asks the user to enter a name and finds all the occurrence of the customer to a passenger list (i.e. passengers). Then prints the name and destination for that passenger in a tabular format (see the output table format)
print: Prints the name and destination values for all passengers in a tabular format (see the output table format)
exit: Exit from the program
The information (i.e. name and destination) regarding the passengers are stored in the passengers list. Each item in this list is also a list where the first item is the name of the passenger and the second item is the destination.
Your Task:
Due to the new requirement, you must change the Del task such that it takes the name of a passenger as input and deletes all occurrences of the passenger from the passengers list. The program also prints the number of occurrences removed from the passengers list. Write the python code to implement this functionality between the green # lines. i.e.
######################################################
######################################################
Hint:
- Create an empty list foundindex.
- Iterate over the passengers list. If the name value of a item is same as the input name, append the index of the row to the foundindex list.
- If the number of items in the foundindex list is greater than 0, do the following. Else, do nothing.
- Iterate over the foundindex list using index, i.
- Delete the row foundindex[i] from the passengers list
- Update foundindex by subtracting 1 from all the values in the list. Write a function to implement this. This function will take a list as a parameter and return a list where the numbers are reduced by 1. (This code will go between the next green # lines that says your function code goes here.)
- Iterate over the foundindex list using index, i.
- Display the number of occurrences removed for the name. Output string format:
Removed <n> occurrences of <name>
———————————————
|Passenger Name | Destination |
———————————————
|A | PHX |
|B | AUS |
|F | LAS |
|A | AUS |
|B | LAS |
|C | PHX |
|A | PHX |
———————————————
Sample Input:
For the passengers given in the above table
Enter passenger name:
A
Sample Output:
Removed 3 occurrences of A
The table after deletion:
———————————————
|Passenger Name | Destination |
———————————————
|B | AUS |
|F | LAS |
|B | LAS |
|C | PHX |
———————————————
Code:
menu_prompt = ”’
Available commands:n
(add) Add passengern
(del) Delete passengern
(find) Find passenger n
(print) Print passenger listn
(exit) Exit the programn
Enter command:n”’
destinations = [‘PHX’, ‘AUS’, ‘LAS’]
destination_prompt = ”’
Available destinations:n
(PHX) Phoenixn
(AUS) Austinn
(LAS) Las Vegasn
Enter destination:n”’
passengers = []
print(‘Welcome to Mohawk Airlines!’)
while True:
user_input = input(menu_prompt).strip().lower()
if user_input == ‘add’:
name = input(‘Enter passenger name:n’).strip().upper()
destination = input(destination_prompt).strip().upper()
while destination not in destinations:
destination = input(destination_prompt).strip().upper()
passengers.append([name, destination])
elif user_input == ‘del’:
name = input(‘Enter passenger name:n’).strip().upper()
# deletes only the first appearance
# for index, row in enumerate(passengers):
# if row[0] == name: # as the first entry is the name
# del passengers[index]
# break
######################################################
############# Code implementing the steps ################
######################################################
elif user_input == ‘find’:
name = input(‘Enter passenger name:n’).strip().upper()
foundindex = []
for index, row in enumerate(passengers):
if row[0] == name:
foundindex.append(index)
if len(foundindex) == 0:
print(“No such customer”)
else:
print(“———————————————“)
print(“|Passenger Name | Destination |”)
print(“———————————————“)
for index in foundindex:
print(‘|{0:20} | {1:20}|’.format(passengers[index][0], passengers[index][1]))
print(“———————————————“)
elif user_input == ‘print’:
print(“———————————————“)
print(“|Passenger Name | Destination |”)
print(“———————————————“)
for passenger in passengers:
print(‘|{0:20} | {1:20}|’.format(passenger[0], passenger[1]))
print(“———————————————“)
elif user_input == ‘exit’:
break
else:
print(‘Unrecognized command.’)
######################################################
########### Your function code goes here. #################
######################################################


0 comments