Question

In Python 3 Create a Car class that has the following characteristics: It has a gas_level...

In Python 3

Create a Car class that has the following characteristics: It has a gas_level attribute. It has a constructor (__init__ method) that takes a float representing the initial gas level and sets the gas level of the car to this value. It has an add_gas method that takes a single float value and adds this amount to the current value of the gas_level attribute. It has a fill_up method that sets the car’s gas level up to 13.0 by adding the amount of gas necessary to reach this level. It will return a float of the amount of gas that had to be added to the car to get the gas level up to 13.0. However, if the car’s gas level was greater than or equal to 13.0 to begin with, then it doesn’t need to add anything and it simply returns a 0. (Note: you can call the add_gas method from within the fill_up method by using this syntax: self.add_gas(amount).)

Reminder: Don’t forget about the self parameter!

Thank You for Your Help!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Car:
    def __init__(self, initial_gas):
        self.gas_level = initial_gas

    def add_gas(self, amount):
        self.gas_level += amount

    def fill_up(self):
        if self.gas_level >= 13:
            return 0
        else:
            amount = 13 - self.gas_level
            self.gas_level = 13
            return amount


car = Car(5)
print('Gas level is at', car.gas_level)
car.add_gas(4)
print('Gas level is at', car.gas_level)
car.fill_up()
print('Gas level is at', car.gas_level)
Add a comment
Know the answer?
Add Answer to:
In Python 3 Create a Car class that has the following characteristics: It has a gas_level...
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
  • In Python Programming: Write a class named Car that has the following data attributes: _ _year_model...

    In Python Programming: Write a class named Car that has the following data attributes: _ _year_model (for the car’s year model) _ _make (for the make of the car) _ _speed (for the car’s current speed) The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed...

  • python Design a class named Car that has the following fields: yearModel: The yearModel field is...

    python Design a class named Car that has the following fields: yearModel: The yearModel field is an Integer that holds the car’s year model. make: The make field references a String that holds the make of the car. speed: The speed field is an Integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: The constructor should accept the car’s year model and make as arguments. These values should be...

  • Create a Python class named Phonebook with a single attribute called entries. Begin by including a...

    Create a Python class named Phonebook with a single attribute called entries. Begin by including a constructor that initializes entries to be an empty dictionary. Next add a method called add_entry that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to the Phonebook object’s dictionary in which the key is the name and the value is the number. For example: >>> book = Phonebook() >>> book.add_entry('Turing', 6173538919) Add...

  • By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise,...

    By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods get Name, addQuiz(score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. (b) Modify the Student class to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as...

  • Python Classes - Write a class to determine the price of a salad and its ingredients....

    Python Classes - Write a class to determine the price of a salad and its ingredients. Write a Salad class. Your class will have two attributes: • name, which should be a string containing the name of the person who ordered the salad. • ingredients, which should be a set of strings where each string is an ingredient in the salad. __init__() The Salad class should have an __init__() method with four parameters, in the following order: • self •...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes,...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes, title...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Using python 3.x to solve this problem. Equilateral Triangle Class EqTri Write a class that has...

    Using python 3.x to solve this problem. Equilateral Triangle Class EqTri Write a class that has several functions related to an Equilateral length : float Triangle. The constructor shouldgetArea): float accept a one parameter for the +getPerimeter): float length (which is the same for all 3+float-0 : float sides in an equilateral triangle) + _str) string -str--() method should return a string such as "Rectangle (Area:50)" float) method should return the area (Same functionality as getArea) Math (area- length?, perim/circum...

  • 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,...

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