Last active
July 21, 2020 07:46
-
-
Save rogerthomas84/571adbe34c89a4a460f20755bbf26623 to your computer and use it in GitHub Desktop.
Simple python script for the kids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The garage stores vehicles. | |
""" | |
class Garage: | |
_vehicles = [] | |
def __init__(self): | |
pass | |
def add_vehicle(self, vehicle): | |
""" | |
Add a vehicle to the garage. | |
:param Vehicle vehicle: | |
""" | |
self._vehicles.append(vehicle) | |
def count_vehicles(self): | |
""" | |
:rtype: int | |
""" | |
return len(self._vehicles) | |
def get_vehicles(self): | |
""" | |
:rtype: list[Vehicle] | |
""" | |
return self._vehicles | |
""" | |
An object representing a vehicle. | |
""" | |
class Vehicle: | |
_vehicle_type = None # type: str | |
_vehicle_colour = None # type: str | |
def __init__(self, vehicle_type, vehicle_colour): | |
""" | |
:param str vehicle_type: | |
:param str vehicle_colour: | |
""" | |
self._vehicle_type = vehicle_type | |
self._vehicle_colour = vehicle_colour | |
def get_vehicle_type(self): | |
""" | |
:rtype: str | |
""" | |
return self._vehicle_type | |
def get_vehicle_colour(self): | |
""" | |
:rtype: str | |
""" | |
return self._vehicle_colour | |
@staticmethod | |
def is_valid_type(provided): | |
""" | |
Is this vehicle type valid? | |
:param str provided: | |
:rtype: bool | |
""" | |
return provided in ['car', 'motorbike', 'pickup', 'lorry'] | |
@staticmethod | |
def is_valid_colour(provided): | |
""" | |
Is this colour valid? | |
:param str provided: | |
:rtype: bool | |
""" | |
return provided in ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] | |
try: | |
garage = Garage() | |
print("----") | |
print("Welcome to the garage!") | |
print("----") | |
while True: | |
needs_car = input("Would you like to add a vehicle? (y or n): ") | |
if needs_car == "y": | |
print("") | |
vehicle_type = input("What type of vehicle do you have? (car, motorbike, pickup or lorry): ") | |
if Vehicle.is_valid_type(vehicle_type) is False: | |
print("You entered an invalid vehicle type. Only car, motorbike, pickup or lorry are valid!") | |
continue | |
vehicle_colour = input("What colour is this " + vehicle_type + "? (red, orange, yellow, green, blue, indigo, violet): ") | |
if Vehicle.is_valid_colour(vehicle_colour) is False: | |
print("You entered an invalid vehicle color.") | |
print("Valid colours are:") | |
print(" red, orange, yellow, green, blue, indigo or violet violet") | |
continue | |
new_vehicle = Vehicle(vehicle_type, vehicle_colour) | |
garage.add_vehicle( | |
new_vehicle | |
) | |
print("") | |
print("OK. I added a {} {} to your garage".format(new_vehicle.get_vehicle_colour(), new_vehicle.get_vehicle_type())) | |
print("") | |
elif needs_car == "n": | |
print("") | |
print("OK. Here's how your garage looks.") | |
count_word = "vehicles" | |
if garage.count_vehicles() == 1: | |
count_word = "vehicle" | |
print("You have {} {} in your garage.".format(garage.count_vehicles(), count_word)) | |
counter = 0 | |
for vehicle in garage.get_vehicles(): | |
counter += 1 | |
print(" {}) A {} {}".format(counter, vehicle.get_vehicle_colour(), vehicle.get_vehicle_type())) | |
exit(1) | |
else: | |
print("") | |
print("Invalid selection...") | |
print("") | |
continue | |
except KeyboardInterrupt as e: | |
print("") | |
print("") | |
print("You cancelled the script.") | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment