在Python3.3中插入错误的插件后重新输入?
如果输入不是1或2,如何重新输入在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,请改用。使用循环
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的身份离开