def test_get_int_input(self): from user_input import get_int_input with mock.patch('builtins.input', side_effect=[ 'hello', '21', '-1234', '1-1', '0', '-0', '4', '10' ]): self.assertEqual( 0, get_int_input(0, 10, "pick a number between 0 and 10: ")) self.assertEqual( 0, get_int_input(0, 10, "pick a number between 0 and 10: ")) self.assertEqual( 4, get_int_input(0, 10, "pick a number between 0 and 10: ")) self.assertEqual( 10, get_int_input(0, 10, "pick a number between 0 and 10: "))
def print_menu_options(self, menu_options): for key, val in menu_options.items(): menu_row = f"| [{key}] {val}" menu_row += f"{' ' * (get_header() - len(menu_row)-1)}|" print(menu_row) print_header() return menu_options[user_input.get_int_input("Option: ")]
def remove_people(): try: user_to_remove = user_input.get_int_input( "Select a person to delete by ID: ") people = load_dictionary_from_file("people") del people[user_to_remove] save_dictionary_to_file("people", people) except: raise Exception("Could not delete person from file")
def change_drink_preference(): clear() list_people() selected_person = user_input.get_int_input("Select person from list above. ID: ") clear() list_drinks() selected_drink = user_input.get_int_input("Select drink from list above. ID: ") try: people = load_dictionary_from_file("people") drinks = load_dictionary_from_file("drinks") people[selected_person].fav_drink = drinks[selected_drink] save_dictionary_to_file("people", people) clear() print_people_menu_title() input("Favourite drink Set. Press enter to continue") except: print("an error occured when setting drinks preference")
def remove_drink(): try: drink_to_remove = user_input.get_int_input("Select a drink to delete by ID: ") drinks = load_dictionary_from_file("drinks") drink_name = drinks[drink_to_remove] del drinks[drink_to_remove] save_dictionary_to_file("drinks", drinks) print(f"{drink_name} succesfully removed") except: raise Exception("Could not remove drink")
def remove_person(): try: user_to_remove = user_input.get_int_input("Select a person to delete by ID: ") people = load_dictionary_from_file("people") person_name = people[user_to_remove] del people[user_to_remove] save_dictionary_to_file("people", people) print(f"{person_name} succesfully removed") except: raise Exception("Could not remove person")
def add_food(self): """this function is soooo ugly""" # temporary max_food = 1000 prompt = """How much food do you want to add? Enter a number between 0 and {0}: """.format(max_food) # ask user how much food they want to add. amount_of_food = get_int_input(0, max_food, prompt) intended_food = amount_of_food # add food in random places if the cell there is dead. attempts_to_add_food = 0 while True: if amount_of_food <= 0: print(f'{intended_food} was added.') break random_x = random.randint(0, self.world_width - 2) random_y = random.randint(0, self.world_height - 2) cell = self.world[random_y][random_x] if (not cell['alive']) and (not cell['has-food']): cell['has-food'] = True amount_of_food -= 1 # if more food is trying to be added than there is space availble for, # prevent that attempts_to_add_food += 1 if attempts_to_add_food > intended_food * 5: message = """ after {0} attempts to add food and {1} food added the world gave up looking for places to add food.""".format( attempts_to_add_food, intended_food - amount_of_food) newlined_print(message) break self.do_what_next()
def do_what_next(self): """Present the user with options, do the option chosen""" # create list of functions to call options = [ self.go_on_living, Game.stop_living, self.add_food, self.save_life, self.load_life, self.restart_life, self.create_dead_world, self.change_environment_variables ] # create input prompt prompt = """What would you like to do next? [1] go on living [2] stop living [3] add food [4] save this life [5] load a saved life [6] restart lifeĀ [7] create dead world [8] change environmental variables enter the number of your choice: """ # ask user to pick a number. use number as index in options. choice = options[get_int_input(1, len(options), prompt) - 1] # call returned function choice()