def load_guitars(): guitars = [] with open("the_guitar_list.csv", "r") as file: read_guitars = list(csv.reader(file)) for temp_guitars in read_guitars: name = temp_guitars[0] year = int(temp_guitars[1]) cost = float(temp_guitars[2]) new_guitar = Guitar(name, year, cost) guitars.append(new_guitar) print(str(len(guitars)) + " guitars loaded") return guitars
def get_guitars(): guitars = list() user_input = input('Add a guitar? (y/n)\n>>>') if user_input == 'y': new_name = input('Enter name of guitar: ') while len(new_name) == 0: new_name = input('Name cannot be blank!\nEnter name of guitar: ') new_year = add_number('integer', 'Enter year guitar was made: ') new_price = add_number('float', 'Enter price of guitar: ') guitars.append(Guitar(new_name, new_year, new_price)) elif len(guitars) == 0: print('You have no guitars')
def add_new_guitar(guitars): while True: try: name = input("name: ") if not name: print("input can not be blank") raise ValueError() for guitar in guitars: if name.lower() == str(guitar.name).lower(): print("the guitar is already in the list") raise ValueError() break except ValueError: continue while True: try: year = input("Year: ") try: year = int(year) except ValueError: print("invalid input; enter a valid year") continue if year < 0: print("The year must be >= 0") raise ValueError() if len(str(year)) < 4 or len(str(year)) > 4: print("the year must have 4 digits; enter a valid year") raise ValueError break except ValueError: continue while True: try: cost = input("Cost: ") try: cost = float(cost) except ValueError: print("invalid input; enter a valid cost") continue if cost < 0: print("The cost must be >= 0") raise ValueError() break except ValueError: continue new_guitar = Guitar(name, year, cost) guitars.append(new_guitar) print("{} ({}): {} added to guitar list".format(name, year, cost)) main_menu(guitars)
def add_new_guitar(guitars): while True: name = input("name: ") year = input("Year: ") cost = input("Cost: ") if not name: if not year: if not cost: list_guitars(guitars) raise SystemExit(0) else: new_guitar = Guitar(name, int(year), int(cost)) guitars.append(new_guitar) print("{} ({}): {} added to guitar list".format(name, year, cost))
""" practical 07 - do from scratch exercise """ from Guitars import Guitar def guitar_test(guitar, expected): got = [guitar.get_age(), guitar.is_vintage()] print("{} get_age() - expected {}. got {}".format(guitar.name, expected[0], got[0])) print("{} is_vintage() - expected {}. got {}".format( guitar.name, expected[1], got[1])) first_guitar = Guitar("Gibson L-5 CES", 1922, 16035.40) guitar_test(first_guitar, [95, "True"]) another_guitar = Guitar("Another Guitar", 2012, 999.99) guitar_test(another_guitar, [5, "False"])
from Guitars import Guitar guitars = [Guitar('Fender Stratocaster', 2014, 765.4), Guitar("Gibson L-5 CES", 1922, 16035.40), Guitar('Line 6 JTV-59', 2010, 1512.9)] def get_guitars(): guitars = list() user_input = input('Add a guitar? (y/n)\n>>>') if user_input == 'y': new_name = input('Enter name of guitar: ') while len(new_name) == 0: new_name = input('Name cannot be blank!\nEnter name of guitar: ') new_year = add_number('integer', 'Enter year guitar was made: ') new_price = add_number('float', 'Enter price of guitar: ') guitars.append(Guitar(new_name, new_year, new_price)) elif len(guitars) == 0: print('You have no guitars') def add_number(message, numtype='float'): try: if numtype == 'integer': user_input = int(input(message)) else: user_input = float(input(message)) except ValueError: print('Number must be a {0}'.format(numtype)) add_number(numtype, message)