Skip to content

Commit

Permalink
show user input in try/except block #39
Browse files Browse the repository at this point in the history
  • Loading branch information
blahosyl committed Apr 16, 2024
1 parent 4aa28b7 commit bed421e
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,30 @@ def select_dish(self):
# validating the input
while True:
try:
# need to separate `input` and `int` to be able to print
# the incorrect user input in the `except` block
# Victor Haffreingue and Zerina Johansson helped me solve this
# ask user to select a dish number
dish_number = int(input(Back.MAGENTA + "Type in the "
+ Fore.GREEN + "number " + Fore.RESET
+ "of the dish you'd like to add:"
+ Back.RESET + " "))
dish_number = input(Back.MAGENTA + "Type in the "
+ Fore.GREEN + "number " + Fore.RESET
+ "of the dish you'd like to add:"
+ Back.RESET + " ")
# convert user input to integer
while dish_number < 1 or dish_number > len(self.dish_data) - 1:
dish_number = int(
input(Back.RED + f'Number "' + Fore.CYAN + f'{dish_number}'
+ Fore.RESET + '" out of range 🤔 '
f"Please type a number between 1 and "
f"{len(self.dish_data) - 1}:"
f"Please type a number between 1 and "
f"{len(self.dish_data) - 1}:"
+ Back.RESET + " "))
break
except ValueError:
# while the input is not one of the allowed options
# ask for input again
print(Back.RED + f"That is not a valid number 🤔 "
f"Please type a whole number between 1 and "
f"{len(self.dish_data) - 1}."
print(Back.RED + '"' + Fore.CYAN + f'{dish_number}' + Fore.RESET
+ f'" is not a valid number 🤔 '
f'Please type a whole number between 1 and '
f'{len(self.dish_data) - 1}.'
+ Back.RESET)

# get the dish with the selected number from the `dishes` list
Expand Down Expand Up @@ -144,9 +149,9 @@ def print_formatted(self, ingredient_list):
# a colon
# the quantity (second item of the list)
print_string = print_string[:opening - 1] \
+ ': ' \
+ f"{ingredient_list[i][1]:g}" \
+ print_string[opening - 1:]
+ ': ' \
+ f"{ingredient_list[i][1]:g}" \
+ print_string[opening - 1:]
# delete the opening and closing parenthesis
print_string = print_string.replace('(', '')
print_string = print_string.replace(')', '')
Expand Down

0 comments on commit bed421e

Please sign in to comment.