def remove_item(self): """ Prints the current inventory and prompts the user to indicate which item to remove. Then removes the item from inventory """ n_rows = len(list(self.inventory.values())[0]) if n_rows == 0: print("No items to remove.") return "Success" print("\nYour current inventory is:") self.print_to_terminal(index=True) index_to_delete = user_inputs.get_string_in_list( prompt= "Enter the index of the item to delete. (or enter quit() to cancel):", err_msg="Enter a valid index.", allowed_vals=[str(index) for index in range(n_rows)], exit_val="quit()") if index_to_delete is None: print("Removal cancelled.") return "Success" index_to_delete = int(index_to_delete) for col in self.inventory.keys(): column_values = self.inventory[col] del column_values[index_to_delete] print("Item removed.") return "Success"
def get_sort_preference(): fields = ["first_name", "last_name", "position", "sep_date"] sort_field = user_inputs.get_string_in_list( prompt=f"What field would you like to sort by? [{', '.join(fields)}]", err_msg="Please select a valid option.", allowed_vals=fields, ).lower() return sort_field
def startup(): difficulty = user_inputs.get_string_in_list( prompt=("Let's play Guess the Number.\n" + "Pick a difficulty level (1, 2, or 3):"), err_msg="Sorry, please choose a valid selection.", allowed_vals=["1", "2", "3"], ) return difficulty
def main(): num = int( user_inputs.get_string_in_list( prompt="Please enter the number of the month (i.e., 1-12):", err_msg="Please enter a value between 1 and 12.", allowed_vals=[str(n) for n in range(1, 13)])) name = month_number_to_name(num) print(f"The name of the month is {name}.")
def main(): tree = initialize_tree() while True: leaf_created = add_leaf(tree) if leaf_created == "quit()": break tree.set_next_node() if tree.next_node is None: tree.next_node = tree.find_empty_nodes(tree.root) if tree.next_node is None: break print("\n\nFinal Decision Tree:") tree.print_tree() proceed = user_inputs.get_string_in_list( prompt="Ready to proceed? [Y/N]:", err_msg="Sorry, please enter a valid value.", allowed_vals=["y", "n"], case_sensitive=False).lower() if proceed == "y": while True: print(tree.next_node.value) response = user_inputs.get_string_in_list( prompt="True or False? [T/F]:", err_msg="Sorry, please enter a valid value.", allowed_vals=["t", "f", "true", "false"], case_sensitive=False, exit_val="quit()").lower()[0] if response == "t": next_node = tree.walk(tree.next_node, "LEFT") elif response == "f": next_node = tree.walk(tree.next_node, "RIGHT") else: break if (next_node.left == next_node.right) and (next_node.left is None): print(next_node.value) break else: print("Goodbye.")
def get_alignment_preference(): options = ["left", "right", "center"] alignment = user_inputs.get_string_in_list( prompt= f"How do you want the final table aligned? [{', '.join(options)}]", err_msg="Please select a valid option.", allowed_vals=options, ).lower() return alignment
def prompt(): """Prompts the user for an action and returns the selection""" allowed_vals = ["Add", "Remove", "Print", "To CSV", "To HTML", "Save", "Exit"] action = user_inputs.get_string_in_list( prompt=f"What would you like to do? [{', '.join(allowed_vals)}]:", err_msg="Sorry, please enter a valid selection", allowed_vals=allowed_vals, case_sensitive=False, ) return action.upper()
def gameover(n_guesses): valid_responses = ["Yes", "No"] print(f"You got it in {n_guesses} guesses!") replay = user_inputs.get_string_in_list( prompt=f"Play again? [{'/'.join(valid_responses)}]", err_msg=f"Please enter a valid response.", allowed_vals=valid_responses, case_sensitive=False).strip().title() if replay == "Yes": return True return False
def perform_action(inventory, action): """Takes a PersonalInventory object and an action. Performs the given action on the object""" if action == "EXIT": inventory.save() print("Closing application. Good bye.") return "EXIT" if action == "ADD": inventory.add_item() elif action == "REMOVE": inventory.remove_item() elif action == "PRINT": inventory.print_to_terminal() elif action == "TO CSV": index = user_inputs.get_string_in_list( prompt=f"Do you want the index included? [Y/N]:", err_msg="Sorry, please enter a valid selection", allowed_vals=["Y", "N"], case_sensitive=False, ) inventory.to_csv(index=(index.upper() == "Y")) elif action == "TO HTML": index = user_inputs.get_string_in_list( prompt=f"Do you want the index included? [Y/N]:", err_msg="Sorry, please enter a valid selection", allowed_vals=["Y", "N"], case_sensitive=False, ) inventory.to_html(index=(index.upper() == "Y")) elif action == "SAVE": inventory.save() else: print("Command not recognized. Try again.") return None return "Success"
def main(): converter = TempConverter() converter.convert_to = user_inputs.get_string_in_list( prompt=("Press 'C' to convert from Fahrenheit to Celsius.\n" + "Press 'F' to convert from Celsius to Fahrenheit.\n" + "Please enter your choice:"), err_msg="Enter a valid selection.", allowed_vals=list(converter.TEMP_SCALES.keys()), case_sensitive=False).upper() converter.temp_type = converter.CONVERT_SCALES[converter.convert_to] converter.temp = user_inputs.get_any_number( prompt= f"Please enter the temperature in {converter.TEMP_SCALES[converter.temp_type]}:", err_msg="Please enter a numeric value.") converted = converter.convert() print( f"The temperature in {converter.TEMP_SCALES[converter.convert_to]} is {converted:.1f}.\n" )
def main(): cart = MultistateTaxes() cart.subtotal = user_inputs.get_positive_number( prompt="What is the order amount?", err_msg="Please enter a valid number.") cart.state = user_inputs.get_string_in_list( prompt="What state do you live in? (Enter the abbreviation):", err_msg="Please enter a valid state abbreviation.", allowed_vals=cart.STATES, case_sensitive=False).upper() cart.county = input( "What county do you live in? (Don't include the word 'county' in your answer): " ).upper() order = cart.checkout() order_summary = "" if order["tax"] != 0: order_summary += f"The tax is ${order['tax']:,.2f}.\n" order_summary += f"The total is ${order['total']:,.2f}." print(order_summary)
def get_input(prompt): return user_inputs.get_string_in_list( prompt=f"{prompt} [Y/N]:", err_msg="Sorry, please enter a valid [Y/N] value.", allowed_vals=["y", "n", "yes", "no"], case_sensitive=False).lower()