在Python中,如果一个输入等于不同的值,是否可以生成while循环?

在Python中,如果一个输入等于不同的值,是否可以生成while循环?,python,while-loop,Python,While Loop,在Python中,我正在编写一个程序,允许用户加密他们输入的消息。他们可以选择两种加密方法,我试图创建一个while循环,如果用户选择的选项不等于两个选项1和2,则会触发该循环 option1 = input('Which encryption method would you like to use? 1 = Across (NOPQ ...) and 2 = Backwards (ZYXW ...)') while option1 != [1, 2]: print 'Please t

在Python中,我正在编写一个程序,允许用户加密他们输入的消息。他们可以选择两种加密方法,我试图创建一个while循环,如果用户选择的选项不等于两个选项1和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()

如果我写这篇文章,那么无论我键入什么,都会要求我输入
1
2

替换
=
不在
中类似,如下例:

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())

基本上,您需要:

option1 = int(raw_input())
然后你想看看它是否在列表中

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

`选项1=int(input())

使用
不在
中而不是
=。请记住,您要测试
选项1
是否在
[1,2]
中,而不是
选项1
是否在
[1,2]
中,并将输入转换为整数。在Python2中这很好,但在Python3中会中断。@Jean Françoisfare或只是生成
1
2
字符串。两种方法都可以。另请参见: