def main(): """Get name, create car object, drive and refuel car.""" name = get_nonempty_string("Enter your car's name: ") car = Car(name, STARTING_FUEL) print(car) print(MENU) choice = input(MENU_PROMPT).lower() while choice != 'q': if choice == 'd': distance = get_positive_float( prompt="How many km do you want to drive? ", negative_error="Distance must be >= 0") distance_driven = car.drive(distance) out_of_fuel_string = " and ran out of fuel" if distance_driven <= car.fuel else "" print(f"{car.name} drove {distance_driven}km{out_of_fuel_string}.") elif choice == 'r': fuel_to_add = get_positive_float( prompt="How many units of fuel do you want to add to the car? ", negative_error="Fuel amount must be >= 0") car.add_fuel(fuel_to_add) print(f"Added {fuel_to_add} units of fuel.") else: print("Invalid menu choice") print(car) print(MENU) choice = input(MENU_PROMPT).lower() print(f"Goodbye {car.name}'s driver.")
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" # 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 assert test_car.fuel == 0 test_car = Car(fuel=10) assert test_car.fuel == 10 def format_sentence(phrase): """ Format sentenecs with grammar. >>> format_sentence("hello") Hello. >>> format_sentence("It is an ex parrot.") It is an ex parrot. >>> format_sentence("one more.") One more. """ if phrase[-1] != ".": phrase += "." phrase.capitalize() return phrase
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" # 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" # 2. write assert statements to show if Car sets the fuel correctly # Test keyword param test_car = Car(fuel=10) assert test_car.fuel == 10 # Test default param test_car = Car("car") assert test_car.fuel == 0
def main(): """Demo test code to show how to use car class.""" my_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("Limo fuel - {}".format(limo.fuel)) limo.drive(115) print("Limo odometer - {}".format(limo.odometer)) print(my_car) print(limo)""" limo = Car("Limo", 100) limo.add_fuel(20) print("Limo fuel - {}".format(limo.fuel)) limo.drive(115) print("Limo odometer - {}".format(limo.odometer)) print(my_car) print(limo)
def main(): print("Let's drive!") car_name = input("Enter your car name: ") my_car = Car(car_name, 100) print(my_car) MENU = """Menu: d) drive r) refuel q) quit """ print(MENU) choice = input("Enter your choice: ").lower() while choice != "q": if choice == "d": prompt_1 = "How many km do you wish to drive? " distance_driven = my_car.drive(valid_number_checker(prompt_1)) if my_car.fuel == 0: print("The car drove {}km and ran out of fuel.".format( distance_driven)) elif my_car.fuel > 0: print("The car drove {}km.".format(distance_driven)) print(my_car) elif choice == "r": prompt_2 = "How many units of fuel do you want to add to the car? " added_fuel = my_car.add_fuel(valid_number_checker(prompt_2)) print("Added {} units of fuel.".format(added_fuel)) print(my_car) else: print("Invalid menu choice") print(MENU) choice = input("Enter your choice: ").lower() print("Good bye The bomb's driver.")
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 assert sort_phrase('hello') == 'Hello.' assert sort_phrase('It is an ex parrot.') == 'It is an ex parrot.' assert sort_phrase('hello world...') == 'Hello world...'
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" # 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 test_one_phrase = phrase_as_sentence("hello") print(test_one_phrase) test_two_phrase = phrase_as_sentence("It is an ex parrot.") print(test_two_phrase) test_three_phrase = phrase_as_sentence("Capitalise no full stop") print(test_three_phrase)
def run_tests(): """Run the tests on the functions.""" # Task 1 # assert test with no message - used to see if the function works properly assert repeat_string("Python", 1) == "Python" print(repeat_string("Python", 1)) # Task 2 # the test below should fail assert repeat_string("hi", 2) == "hi hi" print(repeat_string("hi", 2)) # Task 3 # 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" # Task 4 # 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 # Task 5 test_car = Car() assert test_car.fuel == 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 = Car() assert test_car.fuel == 0 print(format_phrase_to_sentence("bad at grammar")) print(format_phrase_to_sentence("Sentences should end with full stops")) print( format_phrase_to_sentence( "The guy above me is a hypocrite and has been auto-corrected."))
def main(): """Simulate a car drive using Car class.""" print("Let's drive!") name = input("Enter your car name: ") car = Car(name, 100) print(car) print(MENU) choice = input("Enter your choice: ") while choice.lower() != 'q': if choice.lower() == 'd': distance = get_valid_distance() maximum_distance = car.drive(distance) if maximum_distance < distance: print("The car drove {}km and ran out of fuel\n".format( maximum_distance)) else: print("The car drove {}km\n".format(distance)) elif choice.lower() == 'r': refuel = get_valid_refuel() car.add_fuel(refuel) print("Added {} units of fuel.\n".format(refuel)) else: print("Invalid choice\n") print(car) print(MENU) choice = input("Enter your choice: ") print("\nGood Bye {}'s driver.".format(car.name))
def main(): print("Let's drive!") car = Car(input('Enter your car name: '), 100) print(car) print(MENU) menu_choice = input('Enter your choice: ') while menu_choice != 'q': if menu_choice == 'd': distance = get_integer('How many km do you wish to drive? ', 'Distance must be an integer', 'Distance must be >= 0', 0) print('The car drove {}km{}'.format(car.drive(distance), (' and ran out of fuel', '')[bool(car.fuel)])) elif menu_choice == 'r': units_of_fuel = get_integer( 'How many units of fuel do you want to add to the car? ', 'Fuel must be an integer', 'Fuel amount must be >= 0', 0) car.add_fuel(units_of_fuel) print('Added {} units of fuel'.format(units_of_fuel)) elif menu_choice != 'q': print('Invalid choice') print() print(car) print(MENU) menu_choice = input('Enter your choice: ') print("Good bye {}'s driver".format(car.name))
def main(): MENU = "Menu:\nd) drive\nr) refuel\nq) quit" print("Let's Drive!") # my_car = input("Enter your car name: ") my_car = "The Bomb" my_car = Car(my_car, 100) print(my_car) # print(MENU) # choice = input("Enter your choice: ").lower() # while choice != "q": # if choice == "d": # drive_car(my_car) # else: # print("Invalid choice") # print(MENU) # choice = input("Enter your choice: ").lower() # def drive_car(car): distance = int(input("How many km do you wish to drive? ")) print("The car drove {}km".format(my_car.drive(distance))) distance_left = my_car.odometer - distance distance_left = my_car.odometer print( "distance = 20: expect distance to be 80 got {}".format(distance_left)) print("odometer should be 80, got {}".format(my_car.odometer))
def run_tests(): assert repeat_string("Python", 1) == "Python" assert repeat_string("hi", 2) == "hi hi" test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" test_car = Car(fuel=10) assert test_car.fuel == 10 test_car = Car() assert test_car.fuel == 0
def main(): """Demo test code to show how to use car class.""" my_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))
def main(): """Demo test code to show how to use car class.""" my_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))
def run_tests(): """Run the tests on the functions.""" assert repeat_string("Python", 1) == "Python" assert repeat_string("hi", 2) == "hi hi" test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" assert test_car.fuel == 0 test_car = Car(fuel=10) assert test_car.fuel == 10
def main(): """Demo test code to show how to use car class.""" """First Car Instance""" my_car = Car("car", 180) my_car.drive(30) print(my_car) limo = Car("Limo", 100) limo.add_fuel(20) limo.drive(115) print(limo)
def run_tests(): """Run the tests on the functions.""" assert repeat_string("Python", 1) == "Python" assert repeat_string("hi", 2) == "hi hi" test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" test_car = Car(fuel=10) assert test_car.fuel == 10, "Car does not set fuel correctly" assert test_car.fuel != 0, "Car uses initial value rather than passed value"
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" test_car = Car() assert test_car.odometer == 0, "Car does not set odometer correctly" test_car = Car(fuel=10) assert test_car.fuel == 10
def main(): """Test code to used Car class""" my_car = Car(180, "car") my_car.drive(30) limo = Car(100, "Limo") limo.add_fuel(20) # print("fuel =", my_car.fuel) print("odo =", my_car.odometer) print(my_car) print("limo fuel =", limo.fuel) limo.drive(115) print("limo odometer =", limo.odometer) print(limo)
def main(): """Demo test code to show how to use car class.""" my_car = Car("Holden", 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)) # Create a new Car object called "limo" that is initialised with 100 units of fuel. limo = Car("Limo", 100) # print(limo.fuel) # Add 20 more units of fuel to this new car object using the add method. limo.fuel += 20 # Print the amount of fuel in the car. print(limo.fuel) # Attempt to drive the car 115km using the drive method. limo.drive(115) # Print the car's odometer reading. print(limo.odometer) print(limo)
def main(): """Demo test code to show how to use car class.""" my_car = Car("Ferrari", 42, 177) my_car.drive(30) limo = Car("Limo", 100) limo.add_fuel(20) limo.drive(115) print("{self.name}, fuel={self.fuel}, odometer={self.odometer}".format( self=my_car)) print(limo.fuel) print(limo.odometer) print("Car {}, {}".format(my_car.fuel, my_car.odometer)) print("Car {self.fuel}, {self.odometer}".format(self=my_car))
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) """Limo car test""" limo = Car("limo", 100) limo.add_fuel(20) print(limo.fuel) limo.drive(115) print(limo.odometer) print(limo)
def main(): my_car = Car("My car", 180) my_car.drive(30) print("fuel =", my_car.fuel) print("odo =", my_car.odometer) print(my_car) print("Car {self.fuel}, {self.odometer}".format(self=my_car)) limo = Car("Limo", 100) limo.add_fuel(20) print('Limo {}'.format(limo.fuel)) limo.drive(115) print('Limo odo: {}'.format(limo.odometer)) print('Limo {self.fuel}, {self.odometer}'.format(self=limo))
def main(): """Demo test code to show how to use car class.""" my_car = Car("my car", 180) my_car.drive(30) print(my_car) limo = Car("limo", 100) limo.add_fuel(20) limo.drive(115) print(limo.odometer) print(limo)
def main(): """Demo test code to show how to use car class.""" my_car = Car(180, "My Car") my_car.drive(30) limo = Car(100, "Limo") limo.add_fuel(20) limo.drive(115) print("Limo fuel =", limo.fuel) print("Limo odo=", limo.odometer) print("fuel =", my_car.fuel) print("odo =", my_car.odometer) print(my_car) print(limo) print("{} {}, {}".format(my_car.name, my_car.fuel, my_car.odometer)) print("Car {self.fuel}, {self.odometer}".format(self=my_car))
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" assert test_car.fuel == 0 test_car = Car(fuel=10) assert test_car.fuel == 10
def main(): my_car = Car("My car", 180) my_car.drive(30) print("fuel =", my_car.fuel) print("odometer =", 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(limo.fuel) limo.drive(115) print(limo.odometer)
def main(): """Demo test code to show how to use car class.""" my_car = Car('Used Car', 100) 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: {}'.format(limo.name, limo.fuel)) limo.drive(115) print('{} Odometer: {}'.format(limo.name, limo.odometer))
def main(): my_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 {car.fuel}, {car.odometer}".format(car=my_car)) limo = Car("Limo", 100) limo.add_fuel(20) print(limo.fuel) limo.drive(115) print(limo.odometer)
def main(): """Demo test code to show how to use car class.""" my_car = Car(180, "Test Car") my_car.drive(30) print("fuel =", my_car.fuel) print("odo =", my_car.odometer) print(my_car) print() limo = Car(100, "Limo") limo.add_fuel(20) limo.drive(115) print("fuel =", limo.fuel) print("odo =", limo.odometer) print(limo)