Skip to content

Instantly share code, notes, and snippets.

@barangerbenjamin
Created January 8, 2021 12:55
Show Gist options
  • Save barangerbenjamin/0adbfc5aab62de62e36f2ec1137a1690 to your computer and use it in GitHub Desktop.
Save barangerbenjamin/0adbfc5aab62de62e36f2ec1137a1690 to your computer and use it in GitHub Desktop.
print("> Welcome to your Christmas gift list") # Print welcome in the console
gifts = {} # Define an empty list to hold the gifts
running = True # Define a variable to control the loop
while running: # Loop while running is set to a True value
action = input("> Which action [list|add|delete|mark|quit]?\n> ") # Print the menu to the user
if action == "list": # If the user says list
if gifts: # If the gifts dict is not empty
for index, (item, marked) in enumerate(gifts.items()): # Iterate over the iterable of the key/value pairs in the gifts dict
if marked: # If the value of marked is True for the item
print(f"{index + 1} - [X] {item}") # Display with an X
else: # Or
print(f"{index + 1} - [ ] {item}") # Display without an X
else: # gifts has nothing in it
print("No gifts in your list yet, add one!") # Output message to the console
elif action == "add": # If the user says add
item = input("> What item do you wanna add to your list?\n> ") # Ask user what item he/she wants to add and store it in item
gifts[item] = False # Create the key / value pair (item is not marked))
elif action == "delete": # If the user says delete
if gifts: # Check that there is something to delete
item_pos = int(input("> What item do you want to delete from your list? (Give the number)\n> ")) - 1 # Retrieve the number of the item the user wants to delete and offset 1 because we offset 1 during display
if len(gifts) - 1 >= abs(item_pos): # Check that the position is a valid one
list_of_keys = list(gifts.keys()) # Make all the keys within the dict into a list
item = list_of_keys[item_pos] # Retrieve the key (the item) the user wants to delete based on its position
print(f"{item} has been deleted from your list!") # Display message confirming item has been deleted
del gifts[item] # Actually deletes the item
else: # The number given by the user is not a valid one
print("Sorry, this is not in your list! Try again!") # Display message
else: # There is nothing to delete in the gifts dict
print("There is nothing to delete in your list!") # Display message
elif action == "mark": # if the user says mark
if gifts: # Check that there is something to mark
item_pos = int(input("> What item do you want to delete from your list? (Give the number)\n> ")) - 1 # Retrieve the number of the item the user wants to delete and offset 1 because we offset 1 during display
if len(gifts) - 1 >= abs(item_pos): # Check that the position is a valid one
list_of_keys = list(gifts.keys()) # Make all the keys within the dict into a list
item = list_of_keys[item_pos] # Retrieve the key (the item) the user wants to delete based on its position
print(f"{item} has been marked in your list!") # Display message confirming item has been marked
gifts[item] = True # Actually mark the item by overriding the value of that item to True
else: # The number given by the user is not a valid one
print("Sorry, this is not in your list! Try again!") # Display message in console
else: # There is nothing to mark in the dict
print("There is nothing to mark in your list!") # Display message in the console
elif action == "quit": # if the user says quit
running = False # Break the loop by overriding the value of running to something False
else: # When the user says an invalid command
print("> Wrong command! Try again!") # Display message in the console
print("> Goodbye") # We can only reach here when the loop is over, display message in the console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment