0

Here is my code:

while c2 != 1 or 2:
    c2 = input('<1.Enter the kitchen> <2.Exit the house> ')

I was trying to make a text-based rpg, but this part kept stuffing up! I wanted the console to keep asking for the input until the 'c2' variable was 1 or 2, but it kept looping! What am I doing wrong?

mstrap
  • 16,808
  • 10
  • 56
  • 86
huskyT
  • 1

3 Answers3

3

change while c2 != 1 or 2: to

while c2 != 1 and c2 != 2:

or

while c2 not in (1, 2):
gefei
  • 18,922
  • 9
  • 50
  • 67
1

first, there's a similar question here (it's about and. same as or)

second, when doing a multiply conditions you need to separate them like so:

while c2 != 1 and c2 != 2:
    c2 = input('<1.Enter the kitchen> <2.Exit the house> ')

notice that the logics you use while c2 != 1 or 2 becomes a different logic expression

Community
  • 1
  • 1
AranZaiger
  • 61
  • 1
  • 10
0

If you are going to be doing a lot of these type of inputs, then it might make more sense to make a function to deal with this as follows:

def get_response(prompt, replies):
    while True:
        answer = input(prompt)
        if answer in replies:
            return answer

c2 = get_response('<1.Enter the kitchen> <2.Exit the house> ', ['1', '2'])
Martin Evans
  • 45,791
  • 17
  • 81
  • 97