In: Computer Science
Create a Python program that:
Reads the content of a file (Vehlist.txt)
The file contains matching pairs of vehicle models and their
respective makes
Separate out the individual make and model on each line of the
file
Add the vehicle make to one list, and the vehicle model to another
list; such that they are in the same relative position in each
list
Prompt the user to enter a vehicle model
Search the list containing the vehicle models for a match
If a match is found, display the vehicle make by accessing the
vehicle make list entry in the same relative position the match was
found
If no match is found, display a message saying so to the user
Allow the user to enter name for the file
The program should:
Use variables with meaningful names
Display easily understood prompts when collecting user input
Have appropriate comments indicating what each part of the program
is doing
Have a comment area at the beginning of the program identifying the
author of the program and the class it was created for
Save the program as a Python module, and submit the program through
this assignment
Program Code [Python]
# Two lists to store models and makes of vehicles
models = []
makes = []
# Prompting user for name of file
fileName = input('Enter name of file: ')
# Reading file
vehicles = open(fileName, 'r')
for vehicle in vehicles:
# Separating model and make by space and storing in model and make variables
model, make = vehicle.split(' ')
# Adding in respective lists
models.append(model)
makes.append(make)
# Now asking user for a vehicle model
vModel = input('Enter a vehicle model: ')
# Now searching for that model in models list and getting index
index = -1 # Indicates that there is no match
for i in range(0, len(models)):
if models[i] == vModel: # If model is in models list, get its index
index = i
break
if index != -1:
print('Vehicle make is', makes[index])
else:
print('No match found')
Vehlist.txt
Accord Honda
Prius Toyota
Grande Toyota
Corolla Honda
City Honda
Civic Honda
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!