-1

In Python, I am writing a program that allows the user to encrypt messages that they input. There are two methods of encryption that they can select, and I am trying to create a while loop that will trigger if the user chooses an option that is not equal to the two options, 1 and 2.

option1 = input('Which encryption method would you like to use? 1 = Across (NOPQ ...) and 2 = Backwards (ZYXW ...)')
while option1 != [1, 2]:
    print 'Please type 1 or 2.'
    option1 = input()

If I write this, than I will be asked to input 1 or 2 no matter what I type.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87

2 Answers2

1

Replace != with not in like in the example below:

option1 = int(input('Which encryption method would you like to use? 1 = Across (NOPQ ...) and 2 = Backwards (ZYXW ...)'))
while option1 not in [1, 2]:
    print 'Please type 1 or 2.'
    option1 = int(input())
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
zeeks
  • 775
  • 2
  • 12
  • 30
0

Basically, you need to :

option1 = int(raw_input())

then as you want to see if it's in a list

while option1 not in [1,2]:
    print 'Please type 1 or 2.'

` option1 = int(input())

kmcodes
  • 807
  • 1
  • 8
  • 20