Ejemplo n.º 1
0
def main():
    """Demo test code to show how to use car class."""
    my_car = Car("First 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))

    # 1
    limo = Car(100)
    # 2
    limo.add_fuel(20)
    # 3
    print("fuel =", limo.fuel)
    # 4
    limo.drive(115)
    # 5
    print("odo =", limo.odometer)
    # 6 - added to car.py
    print(limo)
    # 7
    limo.name = "Limo"
    limo.fuel = 120
    # 8
    print(limo)
Ejemplo n.º 2
0
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"

    # TODO: 1. fix the repeat_string function above so that it passes the failing test
    # Hint: "-".join(["yo", "yo"] -> "yo-yo"

    # 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"

    # TODO: 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.fuel = 0
    test_car.add_fuel(10)
    assert test_car.fuel == 10

    assert format_sentence("hello") == "Hello."
    assert format_sentence("It is an ex parrot.") == "It is an ex parrot."
Ejemplo n.º 3
0
def main():
    print("Let's Drive!")
    car_name = input("Enter your car name: ")
    user_car = Car(car_name, 100)
    print(user_car)
    print(MENU)
    menu_selection = verify_user_choice(input("Enter your choice: ").lower())
    while menu_selection != 'q':
        if menu_selection == 'd':
            # Drive functionality
            distance_to_drive = int(
                input("How many km do you wish to drive? "))
            while distance_to_drive < 0:
                print("Distance must be >=0")
                distance_to_drive = int(
                    input("How many km do you wish to drive? "))
            if distance_to_drive >= user_car.fuel:
                empty_tank_string = " and ran out of fuel"
                distance_to_drive = user_car.fuel
                user_car.drive(distance_to_drive)
            else:
                empty_tank_string = ""
                user_car.drive(distance_to_drive)
            print("The car drove {}km{}.\n".format(distance_to_drive,
                                                   empty_tank_string))
            print(user_car)
            print(MENU)
            menu_selection = verify_user_choice(
                input("Enter your choice: ").lower())
        else:
            # Refuel functionality
            amount_to_refuel = int(
                input("How many units of fuel do you want to add to the car?"))
            while amount_to_refuel < 0:
                print("Fuel amount must be >= 0")
                amount_to_refuel = int(
                    input(
                        "How many units of fuel do you want to add to the car?"
                    ))
            user_car.fuel = user_car.fuel + amount_to_refuel
            print("Added {} units of fuel.\n".format(amount_to_refuel))
            print(user_car)
            print(MENU)
            menu_selection = verify_user_choice(
                input("Enter your choice: ").lower())
    print("\nGoodbye {}'s driver".format(user_car.name))