def update_fruit_add(L): print("Update fruit") single_loop_print(L) # get index number taken from the printed menu before my_index = get_validated_integer("Please choose the number of the fruit to update: -> ", 0, len(L)) # get amount to add onto the orginal number of fruit in the bowl new_amount = get_validated_integer("Please enter the number of this fruit you are adding to the bowl: -> ", 0, float('inf')) old_amount = L[my_index][0] my_sum = L[my_index][0] + new_amount L[my_index][0] = my_sum final_amount = my_sum output = ("The number of {} has been changed from {} to {}".format(L[my_index][1], old_amount, final_amount)) print(output)
def update_fruitbowl(l): """Take away or add fruit to the fruit bowl. :param l: list :return: None the required updated list is two-dimensional, and the sub list must be of the form [str, int], prints list of fruit with indexes, requests user input for index and new quantity, requests user input for either add or subtract function to run changes quantity for that fruit prints confirmation. """ # test that the list has something in it. if len(l) == 0: print("There is nothing in your fruit bowl") return None print_with_indexes(l) my_index = get_validated_integer( "Please choose the index " "number of the fruit you would " "like to change?", 0, len(l) - 1) add_or_subtract = get_validated_string( "Enter 'A' to add, or any other " "key to subtract fruit: " "-> ", 1, 1).upper() if add_or_subtract == "A": amount_fruit_added = get_validated_integer( "How " "many {}s do you want " "to add?".format(l[my_index][0]), 1, 50) l[my_index][1] += amount_fruit_added print("." * 60) add_confirmation = "You have now added {} {}s to your fruit " \ "bowl!".format(amount_fruit_added, l[my_index][0]) print(add_confirmation) else: amount_fruit_subtracted = get_validated_integer( "How many {}s do you " "want to take " "away?".format(l[my_index][0]), 1, l[my_index][1]) l[my_index][1] -= amount_fruit_subtracted print("." * 60) sub_confirmation = "You have now removed {} {}s from your fruit bowl" \ "!".format(amount_fruit_subtracted, l[my_index][0]) print(sub_confirmation)
def update_fruit_subtract(L): print("Update fruit") single_loop_print(L) # get index number taken from the printed menu before my_index = get_validated_integer("Please choose the number of the fruit to update: -> ", 0, len(L)) old_amount = L[my_index][0] # get amount to subtract from the orginal number of fruit in the bowl new_amount = get_validated_integer("Please enter the number of this fruit you are subtracting from the bowl: -> ", 1, old_amount) my_sum = L[my_index][0] - new_amount if new_amount >= old_amount: my_sum = 0 else: L[my_index][0] = my_sum final_amount = my_sum output = ("The number of {} has been changed from {} to {}".format(L[my_index][1], old_amount, final_amount)) print(output)
def add_fruit(L): fruit = get_string("Please enter the fruit you would like to add: -> ") number = get_validated_integer("How many {}'s are you adding? -> ".format(fruit), 0, math.inf) temp_list = [number, fruit] L.append(temp_list) print("*" * 50) print("You have added {} {}'s to the list".format(number, fruit)) print("Please enter a number.")
def test_fruit_present(l, type_of_fruit): for i in range(0, len(l)): if type_of_fruit.lower() == l[i][0].lower(): fruit_amount = get_validated_integer( "Please enter how much fruit you want to add: ->", 0, 10) print( "You have now added {} new {}s to your collection of {}s which were already in the bowl" .format(fruit_amount, l[i][0], l[i][0])) print("." * 60) # access to original list, then just added one on l[i][1] += fruit_amount return True return False
def fill_fruitbowl(l): """Add fruit to the fruitbowl. :param l: list :return: None The required list must be two-dimensional, and the sub list must be of the form [str, int], requests user input for index and quantity, adds new fruit and quantity to the list, prints confirmation. """ cont = "y" while cont == "y": type_of_fruit = get_validated_string( "What fruit would you " "like to add to the bowl?", 3, 20) result = test_fruit_present(l, type_of_fruit) if result == False: quantity_of_fruit = get_validated_integer( "How many of that fruit would " "you like to add?", 0, math.inf) print("You have entered {} {}s".format(quantity_of_fruit, type_of_fruit)) confirm = input( "press (A) to confirm, (B) to re-enter again or (C) to exit without changes: -> " ) temporary_list = [type_of_fruit, quantity_of_fruit] if confirm == "A": l.append(temporary_list) print("." * 60) print("You have entered {} {}s in the fruit bow" "l!".format(quantity_of_fruit, type_of_fruit)) print("." * 60) return None elif confirm == "B": # go back to start of loop continue elif confirm == "C": return None else: return None
def fill_fruitbowl(L): """ the required list is two-dimensional, and the sub list must be of the form [str, int], requests user input for index and quantity, adds new fruit and quantity to the list, prints confirmation :param L: list :return: None """ type_of_fruit = get_validated_string( "What fruit would you like to add to the bowl?", 3, 20) quantity_of_fruit = get_validated_integer( "How many of that fruit would you like to add?", 0, math.inf) temporary_list = [type_of_fruit, quantity_of_fruit] L.append(temporary_list) print("." * 60) print("You have entered {} {}s in the fruit bowl!".format( quantity_of_fruit, type_of_fruit)) print("." * 60)