Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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:else ValueError:(在本例中特别是ValueError)_Python_Python 3.x_If Statement_While Loop_Try Except - Fatal编程技术网

Python:else ValueError:(在本例中特别是ValueError)

Python:else ValueError:(在本例中特别是ValueError),python,python-3.x,if-statement,while-loop,try-except,Python,Python 3.x,If Statement,While Loop,Try Except,我有一个与我的代码无关的问题。我只是好奇。为什么我(我不知道你)只能在try-and-except循环中使用ValueError?例如: print("What is 1 + 1?") while(True): try: UserInput = int(input(("Your answer here:")) if(UserInput == 2): print("Congratulations you are correct!")

我有一个与我的代码无关的问题。我只是好奇。为什么我(我不知道你)只能在try-and-except循环中使用ValueError?例如:

print("What is 1 + 1?")
while(True):
    try:
        UserInput = int(input(("Your answer here:"))
        if(UserInput == 2):
            print("Congratulations you are correct!")
            break
        else:
            print("That is incorrect. Try again!")
    except ValueError:
        print("That is not a number. Try again!")
这工作得非常好(或者至少应该如此),但是,为什么(如果不是的话)下一段代码不工作呢

print("What is 1 + 1?")
while(True):
    UserInput = int(input("Your answer here:"))
    if(UserInput == 2):
        print("Congratulations you are correct!")
        break
    elif(UserInput != 2):
        print("That is incorrect. Try again!")
    else(ValueError):
        print("That is not a number. Try again!")
当我运行此命令时,会出现以下错误:

Traceback (most recent call last):
  File "python", line 9
    else(ValueError):
        ^
SyntaxError: invalid syntax

我知道这是因为ValueError(我认为)只适用于try和except循环,但为什么不能在上述场景中工作呢?我想他们会给出同样的结果,但是,我不知道一切。也许你们中有一个聪明得惊人的人可以告诉我,我的建议行不通,或者是一个替代方案。感谢您尝试向我澄清:)。

第二个示例中的语法错误来自这样一个事实,
else
不需要任何条件。第一个例子完全可以

更好的方法是,使try块尽可能短:

print("What is 1 + 1?")
while True:
    try:
        UserInput = int(input(("Your answer here:"))
    except ValueError:
        print("That is not a number. Try again!")
    else:
        if UserInput == 2:
            print("Congratulations you are correct!")
            break
        else:
            print("That is incorrect. Try again!")

第二个示例中的SyntaxError来自这样一个事实,
else
不需要任何条件。第一个例子完全可以

更好的方法是,使try块尽可能短:

print("What is 1 + 1?")
while True:
    try:
        UserInput = int(input(("Your answer here:"))
    except ValueError:
        print("That is not a number. Try again!")
    else:
        if UserInput == 2:
            print("Congratulations you are correct!")
            break
        else:
            print("That is incorrect. Try again!")

try
except
是控制流的一种形式。本质上,它意味着
尝试运行此代码,
除非发生异常(例如
ValueError
),否则请执行其他操作

if
else
是控制流的另一种形式。总之,他们的意思是如果一个条件是真的,做点什么<代码>其他
,做其他事情


发生的异常不是一种情况,因此将
else
与类似
ValueError
的异常一起使用是没有意义的。相反,您希望使用
try
/
except
块。

try
except
是控制流的一种形式。本质上,它意味着
尝试运行此代码,
除非发生异常(例如
ValueError
),否则请执行其他操作

if
else
是控制流的另一种形式。总之,他们的意思是如果一个条件是真的,做点什么<代码>其他
,做其他事情


发生的异常不是一种情况,因此将
else
与类似
ValueError
的异常一起使用是没有意义的。相反,您希望使用
try
/
块,但
块除外。

我认为,最后一个else是多余的。因此,else中不能有ValueError。你认为第二种方式会更容易吗?@雅各比:不,不是t@BradTheBrutalitist:您不能用
else
-子句捕获异常。@RUSHYPANCAL OK。谢谢你帮我证明这一点。我真的很感激!:)我认为,最后一个是多余的,所以它不能有值错误。你认为第二种方式会更容易吗?@雅各比:不,不是t@BradTheBrutalitist:您不能用
else
-子句捕获异常。@RUSHYPANCAL OK。谢谢你帮我证明这一点。我真的很感激!:)