-1

I am trying to figure out how to repeat my question after my ending else statement.

My code looks like this at the moment

card = input("What card do you want to delete? ").strip().lower()
if card in monster_card:
    del monster_card[card]
    monster_card.pop(card)
    monster_card.update(card)
elif delete == 'no':
  print("Ok!")

else: 
  print("please enter 'yes' or 'no'")

If I type something other than 'yes' it prints off what it's meant to but then goes to the next question anyways, what I would like to happen is it prints what it's meant to then asks the question again and only continues with 'yes' or 'no'. Has anyone got any suggestions?

Sam
  • 643
  • 2
  • 13
lolz
  • 21
  • 4
  • ou migth want to read: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Aug 21 '23 at 06:28

1 Answers1

2

You could achieve this with a loop structure, such as a while loop. Here's an updated version of your code:

monster_card = {}  # You should define 'monster_card' before using it.

while True:
    card = input("What card do you want to delete? ").strip().lower()
    
    if card in monster_card:
        del monster_card[card]
        monster_card.pop(card)
        monster_card.update(card)
        
    elif card == 'no':
        print("Ok!")
        break  # Exit the loop if 'no' is entered.
    
    else:
        print("please enter 'yes' or 'no'")
Mark
  • 90,562
  • 7
  • 108
  • 148
Don Miller
  • 36
  • 3