The following assignment has to be completed in Python 2 Create class in python
ID: 3694797 • Letter: T
Question
The following assignment has to be completed in Python 2
Create class in python
The class will have a name and an engine value
The constructor will take a string for the name
The constructor will give a random value between 1 and 3 for the engine
The car class will have a function called race that will get a random number between 0 and 10 and multiply it by the engine value.
It will return this value.
The Car will have a __str__() message that will print a nice message out.
In main you will have a list or tuple of 5 cars.
You will call race on all five cars.
You will print the car’s name and their score for the highest value returned from all the race calls (You may need a new tuple or list).
The code is very basic, so it should not take more than 1 page.
Explanation / Answer
Answer for Question:
This below Python 2 code is written based on the given problem statement specfication.
1. Created clas for car with two data members one name an engine value
2. Assign the randome value to engine value
See the below code
import random
class Car:
'Class for Car'
def __init__(self, name):
self.name = name
self.engine_value = random.randint(1,3)
def race(self):
return random.randint(1,10) * engine_value
def __str__(self):
return "Car Name"+self.name+" Score"+self.race()
def main():
object1 = Car("Car 1")
print object1
object2 = Car("Car 2")
print object2
object3 = Car("Car 3")
print object3
object4 = Car("Car 4")
print object4
object5 = Car("Car 5")
print object5
if __name__ == "__main__": main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.