Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add vehicles and compute average year This code below also doesn't work because all it does is restarts: import csv # Create a dictionary to

Add vehicles and compute average year

This code below also doesn't work because all it does is restarts:

import csv

# Create a dictionary to store the vehicle data vehicles = {# Task 1: Use the CSV library to copy all the vehicles from the reader into a Dictionary.with open("cars.csv", "r") as f:    reader = csv.reader(f)    next(reader)  # Skip the header row    for row in reader:        model_year = int(row[0])        vehicle = {            "model_year": model_year,            "type": row[1],            "make": row[2],            "model": row[3],            "transmission": row[4],            "primary_color": row[5]                vehicles[model_year] = vehicle# Task 2: Add the 3 vehicles provided in the instructions to the dictionary.vehicles[2001] = {    "model_year": 2001,    "type": "Car",    "make": "MINI",    "model": "S",    "transmission": "6-speed",    "primary_color": "Red"vehicles[2020] = {    "model_year": 2020,    "type": "Motorcycle",    "make": "Yamaha",    "model": "R6",    "transmission": "6-speed",    "primary_color": "Black"vehicles[2020] = {    "model_year": 2020,    "type": "SUV",    "make": "Chevrolet",    "model": "Suburban",    "transmission": "Automatic",    "primary_color": "Grey"# Task 3: Calculate the average model yeartotal_model_year = sum(vehicle["model_year"] for vehicle in vehicles.values())average_model_year = total_model_year / len(vehicles)# Print the average model yearprint("Average model year:", average_model_year)

*Incorrect code once again from previous post due to syntax issues. Please double check your code and run it with Python IDLE 3.11.6 to make sure that it actually works before responding to this question. I"m still having syntax issues. This most recent screenshot shows syntax issues on line 7 'with. I prefer a screenshot of the code, including an additional screenshot of the output of a successful run using Python IDLE 3.11.6 to verify a correct working code. 


Most recent code:

import csv

# Create a dictionary to store the vehicle data
vehicles = {

# Read the vehicles from the CSV file into the dictionary
with open("cars.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
vehicle = {
"model_year": int(row[0]),
"type": row[1],
"make": row[2],
"model": row[3],
"transmission": row[4],
"primary_color": row[5],

vehicles[row[0]] = vehicle

# Add the additional vehicles to the dictionary
vehicles["2001"] = {
"model_year": 2001,
"type": "Car",
"make": "MINI",
"model": "S",
"transmission": "6-speed",
"primary_color": "Red",


vehicles["2020"] = {
"model_year": 2020,
"type": "Motorcycle",
"make": "Yamaha",
"model": "R6",
"transmission": "6-speed",
"primary_color": "Black",


vehicles["2020"] = {
"model_year": 2020,
"type": "SUV",
"make": "Chevrolet",
"model": "Suburban",
"transmission": "Automatic",
"primary_color": "Grey",


# Calculate the average model year
total_model_year = 0
for vehicle in vehicles.values():
total_model_year += vehicle["model_year"]

average_model_year = total_model_year / len(vehicles)

# Print the average model year
print("Average model year:", average_model_year)

Instructions  

Purpose of this assignment: Add some additional vehicles to the vehicle inventory and compute the average model year: include all of the vehicles in the average, both the ones read from the file and the additional ones added.

For your convenience, find also attached, "cars.csv", which you must read into a dictionary (your code from the previous assignment (Input from CSV file, Dictionary practice) accomplishes this task).

NOTE: Attached is the code files to get you started on this assignment. Use the dropbox link to download the files since it can't be uploaded on here.

What to submit:

Format: A Python .py file (the last 3 characters of the file name should be ".py"). It is not acceptable to submit your code as a screenshot or in a Microsoft Word file.

Content: Your code from the previous assignment (Input from CSV file, Dictionary practice) that I posted and attached here (or modify the other code named readfile.py attached here), along with your changes (the changes are the main part of the work, if you submit code without the changes, it is useless.  Your changes must perform the following tasks:

Task 1: Use the CSV library to copy all the vehicles from the reader into a Dictionary.

Task 2: Add the 3 vehicles here at the end of these instructions into the same dictionary from Task #1.

Task 3: Print the average of the model_year values.  Do not hardcode the model years from the csv file into your code.

model_year: 2001
type: Car
make: MINI
model: S
transmission: 6-speed
primary_color: Red

model_year: 2020
type: Motorcycle
make: Yamaha
model: R6
transmission: 6-speed
primary_color: black
 

model_year: 2020
type: SUV
make: Chevrolet
model: Suburban
transmission: Automatic
primary_color: Grey

 

             

Necessary resources for assignment: (Download from dropbox link below)

                                        

  •    Input from CSV file Dictionary practice.py   https://www.dropbox.com/scl/fi/7tvwbhgquhj6dtbqn2nx8/Input-from-CSV-file-Dictionary-practice.py?rlkey=8wvngyhxu2a7qsxuqvsbuaf0a&dl=0                                                        
  •    readfile.py       https://www.dropbox.com/scl/fi/uo95yofd07mpcomrjlknn/readfile.py?rlkey=tjm6dmbwh6x13rnwdrfhrak6u&dl=0                                             
  •    cars.csv       https://www.dropbox.com/scl/fi/gwjuqgx8g4l4azb4xuhy8/cars.csv?rlkey=6ipvrj1dtis0nhvdomwyka530&dl=0                                                            

*Note as mentioned, you can use either the Input from CSV file dictionary practice.py which was the previous code from last assignment to work with and modify or you can use the readfile.py to work with and modify. It doesn't matter which one. Between which either one you choose to use for the code modification, you will need to also download the cars.csv file.

Step by Step Solution

3.22 Rating (157 Votes )

There are 3 Steps involved in it

Step: 1

Answer import csv Create a dictionary to store the vehicle data vehicles Task 1 Use the CSV library ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

What degrees does the program offer?

Answered: 1 week ago