'User input to stop an if statement within a for loop
I am new to programming and have gotten stuck on a small project. I am trying to have it where I can iterate through a dictionary and determine if the user wants to make an edit. If the user wants to make an edit I want to update the dictionary, otherwise if they do not want to edit the specified section that the program moves on to the next section. I currently have an if statement inside of a for loop to complete this task. The for loop is iterating through the various pieces of a dictionary and the if statement is determining if the user wants to make an edit to the specified portion of the dictionary ( category, recipe name, cooktime, ingredients, instructions ). Currently even if the user inputs the value to say no it continues as if they said yes. What can I do to correct the logic in my code so when the user enters "n" or "N" that it skips over that section of the for loop and continues forward? Below is a snippet of the code. Thank you.
recipes = {1: {'Category' :'Dessert', 'Recipe Name' :' PB Bannana Nice Cream', 'Cooktime' : '15 minutes', 'Ingredients' : '2 frozen bannanas, 2 tablespoons peanut butter powder',
'Instructions' : 'Place frozen bananas into blender with peanut butter powder. Process until creamy'},
2: {'Category' : 'Lunch' , 'Recipe Name' : 'PBJ', 'Cooktime' : '5 minutes', 'Ingredients' : '2 pieces of bread, 2 tablespoons peanut butter, 1 tablespoon jelly or jam',
'Instrutions' :'Spread PB on bread, spread jelly on PB'}}
for number in recipes:
print(f"\n{number}")
for key, value in recipes[number].items():
print(f"\t{key} : {value}")
edit_selection = input('\nPlease select the number that cooresponds to the recipe that you would like to edit: ')
print("")
for key, value in recipes[int(edit_selection)].items():
print(f"\t{key} : {value}")
for key in recipes[int(edit_selection)]:
change_choice = input(f"\nWould you like to edit the recipies {key}? (Y or N) ")
if change_choice == 'y' or 'Y' or "y" or "Y":
print(change_choice)
new_value =input(f"Please enter the new value for the {key}: ")
recipes[int(edit_selection)].update({f'{key}' : f"{new_value}"})
elif change_choice == 'n' or 'N':
break
print(recipes[int(edit_selection)])
Solution 1:[1]
As you might have guessed, the problem is in your if condition. Change it to
if change_choice == 'y' or change_choice == 'Y':
and
elif change_choice == 'n' or change_choice == 'N':
and it should work fine.
If you don't want to have to repeat the comparison with ==, you can instead check whether your input variable's value is in a sequence type (tuple or list, for example) containing all the strings you wish to include. This comparison:
if change_choice in ('y', 'Y')
is equivalent to the above.
Solution 2:[2]
:) Try this:
change_choice = input(f"\nWould you like to edit the recipies {key}? (Y or N) ").title()
if change_choice=="Y":
do something..
else:
break
by adding .title() at the of the input even if the user will type 'y', it will automatically convert to 'Y', because the .title() method returns a string where the first character in every word is upper case
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ahmed Sheta |
