Question

Create a python program based on the following requirements the program should also have to have the following Base Class - C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import sys

""" A class that can be used to represent a car"""
class Car:
"""A simple attempt to represent a car."""

def __init__(self, make, model, year):
"""Initialize attributes for a car."""
self.make = make
self.model = model
self.year = year

def __str__(self):
return self.make + " " + self.model + " " + str(self.year)

def __repr__(self):
return "\nMake: " + self.make + "\nModel: " + self.model + "\nYear: " + str(self.year)


class GasCar(Car):
""" A car thats runs on gas """

def __init__(self, make, model, year, gas_per_mile):
super().__init__(make, model, year)
self.gas_per_mile = gas_per_mile

def __str__(self):
return super().__str__() + " " + str(self.gas_per_mile) + "\n"

def __repr__(self):
return super().__repr__() + "\nGas per mile: " + str(self.gas_per_mile)


class ElectricCar(Car):
""" A car that runs on electric power """

def __init__(self, make, model, year, battery_per_mile):
super().__init__(make, model, year)
self.battery_per_mile = battery_per_mile

def __str__(self):
return super().__str__() + " " + str(self.battery_per_mile) + "\n"

def __repr__(self):
return super().__repr__() + "\nBattery per mile: " + str(self.battery_per_mile)


def read_electric_cars():
""" function to read electric car from the file electric.txt """
with open("electric.txt") as file:
for line in file.readlines():
car_make, car_model, car_year, car_battery_per_mile = line.strip(
'\n').split()
cars.append(ElectricCar(
car_make, car_model, car_year, car_battery_per_mile))


def read_gas_cars():
""" method to read gas car from the file gas.txt """
with open("gas.txt") as file:
for line in file.readlines():
car_make, car_model, car_year, car_gas_per_mile = line.strip(
'\n').split()
cars.append(GasCar(car_make, car_model,
car_year, car_gas_per_mile))


def write_electric_cars():
with open("electric.txt", 'w') as file:
for car in cars:
if isinstance(car, ElectricCar):
file.write(car.__str__())


def write_gas_car(gas_car):
with open("gas.txt", 'a') as file:
for car in cars:
if isinstance(car, gas_car):
file.write(car.__str__())


# car sell manger application
cars = []


def add_car():
""" function to add a new car """

try:
print("\nSelect the type of car\n1. Gas-powered\n2. Electric-powered\n")
car_type = int(
input("Enter your choice: "))

if(car_type == 1):

# read data from the user
car_make = str(input("\nEnter the make of the car: "))
car_model = str(input("Enter the model of the car: "))
car_year = int(input("Enter the year of the car: "))
car_gas_per_mile = int(input("Enter the gas consumed for mile: "))

# create car object and append to cars list
gas_car = GasCar(car_make, car_model, car_year, car_gas_per_mile)
cars.append(gas_car)

elif(car_type == 2):

# read data from the user
car_make = str(input("\nEnter the make of the car: "))
car_model = str(input("Enter the model of the car: "))
car_year = int(input("Enter the year of the car: "))
car_battery_per_mile = int(
input("Enter the battery consumed for mile: "))

# create car object and append to cars list
electric_car = ElectricCar(
car_make, car_model, car_year, car_battery_per_mile)
cars.append(electric_car)
else:
print("Invalid entry")

# exception handling
except ValueError:
print("Value should be an integer")

except:
print("Something went wrong")


def delete_car():
""" function to delete an existing car """

for index in range(len(cars)):
print("Index:", index)
print(cars[index])

try:
index_to_delete = int(input("Enter the index of the car to delete: "))
cars.remove(index_to_delete)

except ValueError:
print("Invalid Index value")

except:
print("Something went wrong")


def view_car():
""" function to display all cars in the user's car book """
print("Cars in the user's car book")
for car in cars:
print(car)


def car_manager_application():

try:
# read the cars in the file
read_electric_cars()
read_gas_cars()

print("*** Car Manager Application ***")
while(True):

# print menu
print("\nSelect an option from the menu")
print("1. Add car")
print("2. View car")
print("3. Delete car")
print("4. Exit program")

choice = int(input("Enter your choice: "))

if(choice == 1):
add_car()

elif(choice == 2):
view_car()

elif(choice == 3):
delete_car()

elif(choice == 4):

# write the cars to the respective files
write_electric_cars()
write_gas_car()
sys.exit(1)

else:
print("Please enter a valid choice")
except ValueError:
print("Value should be an integer")

except:
print("Something went wrong")


if __name__ == "__main__":
car_manager_application()

import sys ** A class that can be used to represent a car class Car: *A simple attempt to represent a car.** def __init__(s

def read_gas_cars(): *** method to read gas car from the file gas.txt** with open(gas.txt) as file: for line in file.read

111 112 car year = int(input(Enter the year of the car: > car_battery_per_mile = int( input(Enter the battery consumed for

165 166 167 168 169 170 171 172 # print menu print(\nSelect an option from the menu) print(1. Add car) print(2. View car

OUTPUT

*** Car Manager Application ***

Select an option from the menu
1. Add car
2. View car
3. Delete car
4. Exit program
Enter your choice: 1

Select the type of car
1. Gas-powered
2. Electric-powered

Enter your choice: 1

Enter the make of the car: Audi
Enter the model of the car: R8
Enter the year of the car: 2019
Enter the gas consumed for mile: 1

Select an option from the menu
1. Add car
2. View car
3. Delete car
4. Exit program
Enter your choice: 1

Select the type of car
1. Gas-powered
2. Electric-powered

Enter your choice: 2

Enter the make of the car: Tesla
Enter the model of the car: Roadster
Enter the year of the car: 2019
Enter the battery consumed for mile: 2

*** Car Manager Application *** Select an option from the menu 1. Add car 2. View car 3. Delete car 4. Exit program Enter you

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.

Add a comment
Know the answer?
Add Answer to:
Create a python program based on the following requirements the program should also have to have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • python code DC Final Project Implement a class Car with the following properties. A car has...

    python code DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. The following two lines are written in a File called FuelEffic.txt (you have to read these from the txt file) Miles per gallon: 20 Tank Size (in gallons): 25 Therefore, based on these...

  • C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions,...

    C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions, files, and arrays. Do NOT use material such as classes. Make sure to add comments** You are to write an ATM Program. The ATM should allow the user to do the following: 1. Create account 2. Log in 3. Exit When they press 1, they are asked for first name (capital first letter). Then it should ask for password. The computer should give the...

  • Use your Car class from the previous Programming Assignment. For this assignment, create a new class...

    Use your Car class from the previous Programming Assignment. For this assignment, create a new class that represents a Used Car Dealership. Your Java program will simulate an inventory system for the dealership, where the employees can manage the car information. Start by creating an array of 10 cars, with an "ID" of 0 through 9, and use the default constructor to create each car. For example, the third car in an array called cars would have ID 2. You...

  • Write a java program that creates a class Car. Class Car contains the following private member...

    Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks: Ask the user to enter make,...

  • please help in python Suppose you have been tasked to write a Python program that will...

    please help in python Suppose you have been tasked to write a Python program that will accomplish the following tasks as related to the daily gas prices for the past year (365 days): • display the five (5) lowest gas prices for the year • display the five (5) highest gas prices for the year • allow the user to search for a specific gas price in the list; you should display a message for whether or not you found...

  • Final PYTHON program: Create a home inventory class that will be used by a National Builder...

    Final PYTHON program: Create a home inventory class that will be used by a National Builder to maintain inventory of available houses in the country. The following attributes should be present in your home class: -private int squarefeet -private string address -private string city -private string state -private int zipcode -private string Modelname -private string salestatus (sold, available, under contract) Your program should have appropriate methods such as: -constructor -add a new home -remove a home -update home attributes At...

  • Using Python. You will create two classes and then use the provided test program to make sure your program works This m...

    Using Python. You will create two classes and then use the provided test program to make sure your program works This means that your class and methods must match the names used in the test program You should break your class implementation into multiple files. You should have a car.py that defines the car class and a list.py that defines a link class and the linked list class. When all is working, you should zip up your complete project and...

  • C++, use the skeleton code to make a program of the following

    c++, use the skeleton code to make a program of the following include <iostream> tinclude <string> using namespace std; class car public: //define your functions here, at least 5 private: string name; int mpg; double price int horsepower; // feel free to add more atributes int main() // create you objects, call your functions // define member functions here For this lab, write a program that does the following: Creates a class based on the car skeleton code (you may...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT