Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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
忽略'内的错误输入;对于';Python中的语句?_Python_For Loop_User Input - Fatal编程技术网

忽略'内的错误输入;对于';Python中的语句?

忽略'内的错误输入;对于';Python中的语句?,python,for-loop,user-input,Python,For Loop,User Input,我试图编写一个程序,循环提示10次或直到输入正确的输入。输入的错误输入不应计入10次尝试输入正确输入的次数。 我不确定是否应该在for语句中保留提示。以下是我到目前为止对输入错误问题的看法: for i in range(10): value=input("Enter a numerical value: ") if value.isdigit()==False: print("Error.") 更好的构造应该是循环 不必循环10次,你可以循环直到你有一个有效的输

我试图编写一个程序,循环提示10次或直到输入正确的输入。输入的错误输入不应计入10次尝试输入正确输入的次数。 我不确定是否应该在for语句中保留提示。以下是我到目前为止对输入错误问题的看法:

for i in range(10):
    value=input("Enter a numerical value: ")
    if value.isdigit()==False:
       print("Error.")

更好的构造应该是循环

不必循环10次,你可以循环直到你有一个有效的输入,即

while True:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else: 
        break
但是,也就是说,如果你只想循环最多10次,那么你所拥有的构造是非常好的-你只需要在输入有效数字时退出循环

for i in range(10):
    value=input("Enter a numerical value: ")
    if value.isdigit()==False:
        print("Error.")
    else:#is a digit
        break
在看到您的评论后,我仍然建议使用while循环,只需添加一个额外的变量(以及对DarinDouglass的评论进行一些更改)

times_correct = 0
while times_corrent < 10:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else:
        times_corrent += 1
times\u correct=0
当次数小于10时:
值=输入(“输入一个数字:”)
如果value.isdigit()==False:
打印“错误”
其他:
次/相关+=1

更好的构造是使用循环

不必循环10次,你可以循环直到你有一个有效的输入,即

while True:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else: 
        break
但是,也就是说,如果你只想循环最多10次,那么你所拥有的构造是非常好的-你只需要在输入有效数字时退出循环

for i in range(10):
    value=input("Enter a numerical value: ")
    if value.isdigit()==False:
        print("Error.")
    else:#is a digit
        break
在看到您的评论后,我仍然建议使用while循环,只需添加一个额外的变量(以及对DarinDouglass的评论进行一些更改)

times_correct = 0
while times_corrent < 10:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else:
        times_corrent += 1
times\u correct=0
当次数小于10时:
值=输入(“输入一个数字:”)
如果value.isdigit()==False:
打印“错误”
其他:
次/相关+=1

更好的构造是使用循环

不必循环10次,你可以循环直到你有一个有效的输入,即

while True:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else: 
        break
但是,也就是说,如果你只想循环最多10次,那么你所拥有的构造是非常好的-你只需要在输入有效数字时退出循环

for i in range(10):
    value=input("Enter a numerical value: ")
    if value.isdigit()==False:
        print("Error.")
    else:#is a digit
        break
在看到您的评论后,我仍然建议使用while循环,只需添加一个额外的变量(以及对DarinDouglass的评论进行一些更改)

times_correct = 0
while times_corrent < 10:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else:
        times_corrent += 1
times\u correct=0
当次数小于10时:
值=输入(“输入一个数字:”)
如果value.isdigit()==False:
打印“错误”
其他:
次/相关+=1

更好的构造是使用循环

不必循环10次,你可以循环直到你有一个有效的输入,即

while True:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else: 
        break
但是,也就是说,如果你只想循环最多10次,那么你所拥有的构造是非常好的-你只需要在输入有效数字时退出循环

for i in range(10):
    value=input("Enter a numerical value: ")
    if value.isdigit()==False:
        print("Error.")
    else:#is a digit
        break
在看到您的评论后,我仍然建议使用while循环,只需添加一个额外的变量(以及对DarinDouglass的评论进行一些更改)

times_correct = 0
while times_corrent < 10:
    value = input("Enter a number:")
    if value.isdigit() == False:
        print "Error"
    else:
        times_corrent += 1
times\u correct=0
当次数小于10时:
值=输入(“输入一个数字:”)
如果value.isdigit()==False:
打印“错误”
其他:
次/相关+=1
当您获得正确的输入时,需要:

for i in range(10):
    value=input("Enter a numerical value: ")
    if not value.isdigit():  # PEP8 says to use `not` instead of `== False`
        print("Error.")
    else:
        break  # If we get here, the input was good.  So, break the loop.
请参见下面的演示:

>>> for i in range(10):
...     value=input("Enter a numerical value: ")
...     if not value.isdigit():
...         print("Error.")
...     else:
...         break
...
Enter a numerical value: a
Error.
Enter a numerical value: b
Error.
Enter a numerical value: 12
>>>
当您获得正确的输入时,您需要:

for i in range(10):
    value=input("Enter a numerical value: ")
    if not value.isdigit():  # PEP8 says to use `not` instead of `== False`
        print("Error.")
    else:
        break  # If we get here, the input was good.  So, break the loop.
请参见下面的演示:

>>> for i in range(10):
...     value=input("Enter a numerical value: ")
...     if not value.isdigit():
...         print("Error.")
...     else:
...         break
...
Enter a numerical value: a
Error.
Enter a numerical value: b
Error.
Enter a numerical value: 12
>>>
当您获得正确的输入时,您需要:

for i in range(10):
    value=input("Enter a numerical value: ")
    if not value.isdigit():  # PEP8 says to use `not` instead of `== False`
        print("Error.")
    else:
        break  # If we get here, the input was good.  So, break the loop.
请参见下面的演示:

>>> for i in range(10):
...     value=input("Enter a numerical value: ")
...     if not value.isdigit():
...         print("Error.")
...     else:
...         break
...
Enter a numerical value: a
Error.
Enter a numerical value: b
Error.
Enter a numerical value: 12
>>>
当您获得正确的输入时,您需要:

for i in range(10):
    value=input("Enter a numerical value: ")
    if not value.isdigit():  # PEP8 says to use `not` instead of `== False`
        print("Error.")
    else:
        break  # If we get here, the input was good.  So, break the loop.
请参见下面的演示:

>>> for i in range(10):
...     value=input("Enter a numerical value: ")
...     if not value.isdigit():
...         print("Error.")
...     else:
...         break
...
Enter a numerical value: a
Error.
Enter a numerical value: b
Error.
Enter a numerical value: 12
>>>


谢谢,但我相信我的措辞很糟糕。我实际上有几个错误检查,如果用户提供了我认为不正确的输入,例如ifvalue.isdigit()==False,我希望该用户输入不算作“for”语句的输入。基本上,我希望用户能够无限期地输入错误的输入,但只能输入正确的输入10次。@用户,在这种情况下,我仍然建议使用while循环——请参阅我的edit此处不需要无限循环。只需使用
while time\u correct<10
。另外,
if value.isdigit()==False
很冗长,使用
if not value.isdigit()
@DarinDouglass-我倾向于同意第一点-这样做确实更有意义,但是,我没有更改值。isdigit==False-看起来更容易阅读(至少对我来说)即使它更冗长,更容易出错。我明白我现在做错了什么,我应该从一开始就使用“while”,因为语句依赖于用户输入。非常感谢,你真的帮了我很多忙。谢谢你,但我相信我的措辞很糟糕。我实际上有几个错误检查用户是否提供了我认为不正确的输入,例如ifvalue.isdigit()==False,我希望该用户输入不算作“for”语句的输入。基本上,我希望用户能够无限期地输入错误的输入,但只能输入正确的输入10次。@用户,在这种情况下,我仍然建议使用while循环——请参阅我的edit此处不需要无限循环。只需使用
while time\u correct<10
。另外,
if value.isdigit()==False
很冗长,使用
if not value.isdigit()
@DarinDouglass-我倾向于同意第一点-这样做确实更有意义,但是,我没有更改值。isdigit==False-看起来更容易阅读(至少对我来说)即使它更冗长,更容易出错。我明白我现在做错了什么,我应该从一开始就使用“while”,因为语句依赖于用户输入。非常感谢,你真的帮了我很多忙。谢谢你,但我相信我的措辞很糟糕。我实际上有几个错误检查,如果用户提供了我认为不正确的输入,例如ifvalue.isdigit()==False,我希望该用户输入不算作“for”语句的输入。基本上,我希望用户能够键入不正确的输入索引