Question
Please use Python exception handling (with try, except) in following code to handle multiple possible exception # Function to take vehicle type def vehicleType(): #
Please use Python exception handling (with try, except) in following code to handle multiple possible exception
# Function to take vehicle type def vehicleType(): # Loop till user enter valid vehicle type while True: # Take vehicle type data ['vehicle'] = input(" Type of vehicle(Car, Bus, Truck)? ").lower() # If vehicle type is not car or bus or truck if not data ['vehicle'] in ('car', 'bus', 'truck'): # Print an error message & agian take vehicle type print("Please enter valid vehicle type... ") # If vehicle type is car or bus or truck else: # Exit from loop break # Return vehicle type
# Function to take hour the vehicle entered the lot def hourEntered(): # Loop till user enter valid hour while True: # Take hour the vehicle entered the lot data ['inHour']= int(input("Hour vehicle entered lot(0-24)? ")) # If hour is less than 0 or greater than 24 if data ['inHour'] < 0 or data ['inHour'] > 24: # Print an error message print("Please enter valid hour entered... ") # If hour is between 0 and 24 else: # Exit from loop break
# Function to take minute the vehicle entered the lot def minEntered(): # Loop till user enter valid minute while True: # Take minute the vehicle entered the lot data['inMin'] = int(input("Minute vehicle entered lot(0-60)? ")) # If minute is less than 0 or greater than 60 if data ['inMin'] < 0 or data ['inMin'] > 59: # Print an error message print("Please enter valid minute entered... ") # If minute is between 0 and 60 else: # Exit from loop break
# Function to take hour the vehicle left the lot def hourOut(): # Loop till user enter valid hour while True: # Take hour the vehicle left the lot data ['outHour'] = int(input("Hour vehicle left lot(0-24)? ")) # If hour is less than 0 or greater than 24 if data ['outHour'] < 0 or data ['outHour'] > 24: # Print an error message print("Please enter valid hour out... ") # If hour is between 0 and 24 else: # Exit from loop break
# Function to take minute the vehicle left the lot def minOut(): # Loop till user enter valid minute while True: # Take minute the vehicle left the lot data ['outMin'] = int(input("Minute vehicle left lot(0-60)? ")) # If minute is less than 0 or greater than 60 if data ['outMin'] < 0 or data ['outMin'] > 59: # Print an error message print("Please enter valid minute out... ") # If minute is between 0 and 60 else: # Exit from loop break
def ParkingCharge(): #Function to calculate Parking Charges charge=0 if(data['vehicle'] == "car"): # when vehicle is car if(data['Total_p'] > 3): charge = 1.50 * data['Total_p'] elif(data['vehicle'] == "truck"): # when vehicle is truck if(data['Total_p'] <= 2): charge = data['Total_p']*1.00 else: charge = data['Total_p']*2.30 elif(data['vehicle'] == "bus"): # when vehicle is bus if(data['Total_p'] == 1): charge=data['Total_p']*2.00 else: charge=data['Total_p']*3.70 data['charge'] = charge #returning charges
# Total function def Total():
# Convert total in time to minutes data['TotalInTime'] = data['inHour']*60 +data['inMin'] # Convert out time to minutes data['TotalOutTime'] = data['outHour']*60 + data['outMin'] # Calculate parking time data['ParkingTime'] = data['TotalOutTime']- data ['TotalInTime'] # Calculate parking hour data['Parking_Hour'] = data['ParkingTime']//60 # Calculate parking minute data['Parking_Min'] = data['ParkingTime']%60 # Rounded time data['Total_p'] = data['Parking_Hour'] # If parking minute is greater than 0 if(data['Parking_Min'] > 0): # Rounded time data['Total_p'] = data['Parking_Hour'] + 1 else: data['Total_p'] = data['Parking_Hour'] def file_handling(final): with open ('file.txt', 'a') as f: f.write(final) def printer(): final = f''' ---PARKING LOT CHARGES--- Type of vehicle:{data['vehicle']} TIME-IN : {data['inHour']}:{data['inMin']} TIME-OUT: {data['outHour']}:{data['outMin']} PARKING TIME: {data['Parking_Hour']}:{data['Parking_Min']} rounded total :{data['Total_p']} TOTAL CHARGES: {data['charge']} ''' print(final) return final
flag = 'yes' while flag == 'yes': data = {} #function calls vehicleType() # Call function ot take hour vehicle entered hourEntered() # Call function to take minute vehicle entered minEntered() # Call function to take hour vehicle left hourOut() # Call function to take minutes vehicle left minOut() Total() ParkingCharge() cont = printer() file_handling(cont)
flag = input ('Do you want to contionue? yes or not?').lower()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started