Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3.3中插入错误的插件后重新输入?_Python_Input_Python 3.3 - Fatal编程技术网

在Python3.3中插入错误的插件后重新输入?

在Python3.3中插入错误的插件后重新输入?,python,input,python-3.3,Python,Input,Python 3.3,如果输入不是1或2,如何重新输入 status = eval(input("Are they single(1) or married(2)? \n")) 使用循环: while True: status = input("Are they single(1) or married(2)? \n") if status in ('1', '2'): break 顺便说一句,不要使用eval。它可以计算任意表达式。如果要将输入字符串转换为int,请改用。使用循环

如果输入不是1或2,如何重新输入

status = eval(input("Are they single(1) or married(2)? \n"))
使用循环:

while True:
    status = input("Are they single(1) or married(2)? \n")
    if status in ('1', '2'):
        break
顺便说一句,不要使用
eval
。它可以计算任意表达式。如果要将输入字符串转换为int,请改用。

使用循环:

while True:
    status = input("Are they single(1) or married(2)? \n")
    if status in ('1', '2'):
        break
顺便说一句,不要使用
eval
。它可以计算任意表达式。如果要将输入字符串转换为int,请改用。

使用循环:

while True:
    status = input("Are they single(1) or married(2)? \n")
    if status in ('1', '2'):
        break
顺便说一句,不要使用
eval
。它可以计算任意表达式。如果要将输入字符串转换为int,请改用。

使用循环:

while True:
    status = input("Are they single(1) or married(2)? \n")
    if status in ('1', '2'):
        break

顺便说一句,不要使用
eval
。它可以计算任意表达式。如果要将输入字符串转换为int,请改用。

@falsetru已经用我的首选方式进行了回答。这里有几个你可以在别处看到的备选方案

status = input("Are they single(1) or married(2)? \n")
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
这是不可取的,因为提示重复

status = -1 # or 0 or None or some other invalid value
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
我认为这很难看,但有些人不喜欢使用
break


我喜欢@falsetru版本的一个原因是,当您想要计算/限制重试次数时,很容易将其转换为
for
循环。

@falsetru已经用我喜欢的方式回答了。这里有几个你可以在别处看到的备选方案

status = input("Are they single(1) or married(2)? \n")
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
这是不可取的,因为提示重复

status = -1 # or 0 or None or some other invalid value
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
我认为这很难看,但有些人不喜欢使用
break


我喜欢@falsetru版本的一个原因是,当您想要计算/限制重试次数时,很容易将其转换为
for
循环。

@falsetru已经用我喜欢的方式回答了。这里有几个你可以在别处看到的备选方案

status = input("Are they single(1) or married(2)? \n")
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
这是不可取的,因为提示重复

status = -1 # or 0 or None or some other invalid value
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
我认为这很难看,但有些人不喜欢使用
break


我喜欢@falsetru版本的一个原因是,当您想要计算/限制重试次数时,很容易将其转换为
for
循环。

@falsetru已经用我喜欢的方式回答了。这里有几个你可以在别处看到的备选方案

status = input("Are they single(1) or married(2)? \n")
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
这是不可取的,因为提示重复

status = -1 # or 0 or None or some other invalid value
while status not in ('1', '2'):
    status = input("Are they single(1) or married(2)? \n")
我认为这很难看,但有些人不喜欢使用
break


我喜欢@falsetru版本的一个原因是,当您想要计算/限制重试次数时,可以很容易地转换为
for
循环。

在这种情况下,转换为
int
意味着您可能会引发异常。最好只保留为
str
,在这种情况下,转换为
int
意味着您可能会引发异常。最好只保留为
str
,在这种情况下,转换为
int
意味着您可能会引发异常。最好只保留为
str
,在这种情况下,转换为
int
意味着您可能会引发异常。最好以
str的身份离开