88.5k views
2 votes
Write codes to create a Vehicle with a constructor that will set data attributes, maxSpeed and mileage to the parameters defined in the .constructor

Write an object that will refer to the class (python)

User Wei Shi
by
8.0k points

1 Answer

4 votes
class Vehicle:
def __init__(self, maxSpeed, mileage):
self.maxSpeed = maxSpeed
self.mileage = mileage

# prompt the user for input
maxSpeed = int(input("Enter the maximum speed of the vehicle: "))
mileage = int(input("Enter the mileage of the vehicle: "))

# create a vehicle object with the user input
car = Vehicle(maxSpeed, mileage)

# print out the vehicle's attributes
print("Vehicle max speed:", car.maxSpeed)
print("Vehicle mileage:", car.mileage)


In this example, we're using the input() function to prompt the user for input, converting the input to integers using the int() function, and passing the input values as arguments to the Vehicle constructor. We're then creating a car object and printing out its maxSpeed and mileage attributes using the print() function.
User Farcaller
by
7.6k points