def drive(self, distance): """Similar to parent Car, but pass in reliability attribute.""" if random.uniform(1, 100) < self.reliability: distance_driven = Car.drive(self, distance) else: distance_driven = 0 return distance_driven
def main(): """Demo test code to show how to use car class.""" my_car = Car("Car", 180) my_car.drive(30) print("fuel =", my_car.fuel) print("odo =", my_car.odometer) print(my_car) print("Car {}, {}".format(my_car.fuel, my_car.odometer)) print("Car {self.fuel}, {self.odometer}".format(self=my_car)) limo = Car("Limo", 100) limo.add_fuel(20) print("fuel =", limo.fuel) limo.drive(115) print("odo =", limo.odometer) print(str(limo))
def run_tests(): """Run tests to show workings of Car and Taxi classes.""" bus = Car("Datsun", 180) bus.drive(30) print("fuel =", bus.fuel) print("odo =", bus.odometer) bus.drive(55) print("fuel =", bus.fuel) print("odo = ", bus.odometer) print(bus) # drive bus (input/loop is oblivious to fuel) distance = int(input("Drive how far? ")) while distance > 0: travelled = bus.drive(distance) print("{} travelled {}".format(str(bus), travelled)) distance = int(input("Drive how far? ")) t = Taxi("Prius 1", 100) print(t) t.drive(25) print(t, t.get_fare()) t.start_fare() t.drive(40) print(t, t.get_fare()) sst = SilverServiceTaxi("Limo", 100, 2) print(sst, sst.get_fare()) sst.drive(10) print(sst, sst.get_fare())
def run_tests(): bus = Car("Datsun", 180) bus.drive(30) print("fuel =", bus.fuel) print("odo =", bus.odometer) bus.drive(55) print("fuel =", bus.fuel) print("odo = ", bus.odometer) print(bus) distance = int(input("Drive how far? ")) while distance > 0: travelled = bus.drive(distance) print("{} travelled {}".format(str(bus), travelled)) distance = int(input("Drive how far? ")) new_taxi = Taxi("Prius 1", 100) print(new_taxi) new_taxi.drive(25) print(new_taxi, new_taxi.get_fare()) new_taxi.start_fare() new_taxi.drive(40) print(new_taxi, new_taxi.get_fare()) new_silver_service_taxi = SilverServiceTaxi("Limo", 100, 2) print(new_silver_service_taxi, new_silver_service_taxi.get_fare()) new_silver_service_taxi.drive(10) print(new_silver_service_taxi, new_silver_service_taxi.get_fare())
def tests(): car = Car("Datsun", 180) car.drive(30) print("fuel =", car.fuel) print("odo =", car.odometer) car.drive(55) print("fuel =", car.fuel) print("odo = ", car.odometer) print(car) distance = int(input("Drive how far? ")) while distance > 0: travel = car.drive(distance) print("{} travelled {}".format(str(car), travel)) distance = int(input("Drive how far? ")) taxis = Taxi("Prius 1", 100) taxis.drive(25) print(taxis) print(taxis, taxis.get_fare()) taxis.start_fare() taxis.drive(40) print(taxis, taxis.get_fare()) silver_service_taxi = SilverServiceTaxi("Limo", 100, 2) print(silver_service_taxi, silver_service_taxi.get_fare()) silver_service_taxi.drive(10) print(silver_service_taxi, silver_service_taxi.get_fare())
def run_tests(): """Run the tests on the functions.""" # assert test with no message - used to see if the function works properly assert repeat_string("Python", 1) == "Python" # the test below should fail assert repeat_string("hi", 2) == "hi hi" # Hint: "-".join(["yo", "yo"] -> "yo-yo" # used to see if Car's init method sets the odometer correctly test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" # Note that Car's __init__ function sets the fuel in one of two ways: # using the value passed in or the default # You should test both of these test_car = Car(fuel=10) assert test_car.fuel == 10 test_car = Car(fuel=0) assert test_car.fuel == 0, "Car does not set fuel correctly"
def run_tests(): """Run the tests on the functions.""" # assert test with no message - used to see if the function works properly assert repeat_string("Python", 1) == "Python" # the test below should fail assert repeat_string("hi", 2) == "hi hi" # assert test with custom message, # used to see if Car's init method sets the odometer correctly # this should pass (no output) test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" # 2. write assert statements to show if Car sets the fuel correctly # Note that Car's __init__ function sets the fuel in one of two ways: # using the value passed in or the default # You should test both of these test_car = Car(fuel=10) assert test_car.fuel == 10 test_car = Car() assert test_car.fuel == 0
def main(): user_name = input("Let's drive!\nEnter your car name:") car = Car(user_name, 100) menu_selection = "" while menu_selection != "q": print(car) menu_selection = menu("Menu:\nd) drive\nr) refuel\nq) quit") if menu_selection == "d": drive(car) elif menu_selection == "r": refuel(car) print("Good bye {}'s driver.".format(user_name))
def run_tests(): """Test the Car and Taxi class to show they're working.""" # Test Car Class ferrari = Car("Ferrari", 1000) ferrari.drive(333) print("Ferrari fuel:{}".format(ferrari.fuel)) print("Ferrari odometer:{}".format(ferrari.odometer)) ferrari.drive(200) print("Ferrari fuel after driving another 200km:{}".format(ferrari.fuel)) print("Ferrari odo after driving another 200km:{}".format(ferrari.odometer)) print(ferrari) # Test Taxi Class truck = Taxi("Hyundai", 500) truck.start_fare() truck.drive(333) print("Current fare distance of truck is{}".format(truck.current_fare_distance)) print("Fare of Taxi Hyundai after driving 333 is {}".format(truck.get_fare())) # Test SilverServiceClass truck = SilverServiceTaxi("Hyundai", 500, 1) truck.start_fare() truck.drive(333) print("Fare of SilverServiceTaxi Hyundai after driving 333 is {}".format(truck.get_fare()))
def __init__(self, name, fuel, reliability): Car.__init__(self, name, fuel) self.reliability = reliability
def drive(self, distance): start = random.randint(0, 100) if start < self.reliability: Car.drive(self, distance) else: print("Car didn't start")