In: Computer Science
make a python program which forms caravan occupancy report
the output should be in a table format which shows the occupants, number of caravans and the percentage.
caravans with over 9 occupants are considered to be over crowded, and are to be output under a single column (>9).
Program:
from prettytable import PrettyTable
caravan = [] #list for holding number of occupants
overcrowded = [] #list for holding overcrowded information
no_of_occupants=0
no_of_caravan = input("Enter number of caravan:") #get number of
caravan
#for every caravan run the loop
for x in range(no_of_caravan):
occupants = input("Enter occupants") #get number of occupants as
input
caravan.append(occupants)
if occupants> 9: #if occupants are above 9, mark as
overcrowded
overcrowded.append("Yes")
else:
overcrowded.append("No")
no_of_occupants=occupants+no_of_occupants #add occupants to find
total occupants
percentage = (no_of_occupants/(no_of_caravan*9)*100) #calculate
percentage
#display output in table format using PrettyTable
module
t = PrettyTable(['Occupants', 'Number of caravan', 'Percentage',
'Overcrowded'])
#for every caravan run the loop
for x in range(no_of_caravan):
t.add_row([caravan ,no_of_caravan, percentage , overcrowded]) #add
row
print(t) #print the table
Explanation:
i) The program uses prettytable module to get output in table format.
ii) The program gets number of caravan from the user and runs the loop for no_of_caravan times.
iii) It gets the number of occupant and calculate total occupants and percentage ( no_of_occupants/(no_of_caravan*9)*100 ).
iv) The program uses for loop of no_of_caravan times executable to add rows to the table .
v) The expected output is achieved