0

I have this code:

direction = input("Do you go left, right, up or down?! Forwards would also be an option: ")
  
if direction == "left":
    print("You turn left and are confronted by a group of 5 goblins.")
    fight_goblin = input('''You have two choices, die, or beat them to death with their own weapons.
    Which one do you choose insolent one? Use 1 or 2 to select: ''')
    if fight_goblin == "1":
        print('''You turn and run, but, alas, the goblins have legs.
        They run up to you and beat you to death. RESTART!''')
    elif fight_goblin == "2":
        print('''The first goblin rushes towards you menacingly.
        You grab his mace and throw him behind you. The mace falls into your hand.
        The second and third goblin, now enraged, speed towards you; spears in hand.
        You slam the mace into the face of one of the goblins and he falls backwards
        killing his companion in the aftermath.
        The remaining goblins flee in terror.
        BATTLE WON! You collect obvious resources from the goblins bodies.
        +1 Mace +1 spear +200 gold.''')

But when I run it people can type in other stuff and it will just freeze. I know how to restrict words and letters but how do I restrict Numbers? I restricted the words like this:

while True:
    pick = input("Do you pick up the slab? Yes/No ").lower()
    if pick == 'yes':
        print("You pick up the slab and begin reading.")
        break
    elif pick == 'no':
        print("You walk forwards and land facefirst onto the slab.")
        break
    else:
        print("You have to choose Yes or No")
martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

1

Create a list of expect response like this:

accepted_response = ['1', '2']

user_input = input('Keyin 1 or 2: ')
#Then check if the user input is in the accepted response
while user_input not in accepted_response:
    user_input = input('Plesea Keyin 1 or 2: ')
else:
    print('Accepted')
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
  • This generates an infinite loop of input until any of the accepted values is entered. You may replace you intended action after the accepted value is received in the `else` block – Seyi Daniel Aug 21 '20 at 01:01
  • Seyi Daniel That would not work as I need it to display different messages depending on what they respond with – Assassin_Dragon Aug 25 '20 at 04:00
0

store the choice in a while loop first, You really dont need break statements if you use this and if the user enters something else it will stay in the loop. I do this for most of my programs and it works

slabChoice = False
while slabChoice == False:
    pick = input("Do you pick up the slab? Yes/No ").lower()
    if pick == 'yes':
        print("You pick up the slab and begin reading.")
        slabChoice = True
    elif pick == 'no':
        print("You walk forwards and land facefirst onto the slab.")
        slabChoice = True
    else:
        print("You have to choose Yes or No")
        slabChoice = False
SpacedOutKID
  • 95
  • 1
  • 14