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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3_Python_Python 3.x - Fatal编程技术网

为什么我的尝试和尝试没有正常工作?python 3

为什么我的尝试和尝试没有正常工作?python 3,python,python-3.x,Python,Python 3.x,我不知道为什么我的尝试和尝试不能正常工作。我希望它在用户输入字符串“y”或“n”时继续,但如果用户没有输入任何字符串,则打印错误 输出: userInput = input("Are both players ready to start? (y/n): ") k (userInput) Here we go! 预期产出: userInput = input("Are both players ready to start? (y/n): ") k (userInput) Wrong Inp

我不知道为什么我的尝试和尝试不能正常工作。我希望它在用户输入字符串“y”或“n”时继续,但如果用户没有输入任何字符串,则打印错误

输出:

userInput = input("Are both players ready to start? (y/n): ")
k (userInput)
Here we go!

预期产出:

userInput = input("Are both players ready to start? (y/n): ")
k (userInput)
Wrong Input, Try Again


代码中没有出现错误,因此永远不会调用
except
。如果要引发自己的异常,请使用
raise
关键字。此处的详细信息:

您的代码中没有出现错误,因此永远不会调用
except
。如果要引发自己的异常,请使用
raise
关键字。此处的详细信息:

在try块中,您没有引发错误,因此没有要捕获的内容。增加工资就可以了

userInput = input("Are both players ready to start? (y/n): ")
try:
  if userInput == "y":
    print("Here we go! ")
    print()
  elif userInput == "n":
    print("Too bad we're starting anyways")
  else:
    raise ValueError("What's up with that?")
except:
  print("Wrong Input Try Again")
按照@ctrl-alt-delor的建议,您也可以跳过
try/except
块,只使用
if/else
块。此代码段应执行相同的操作:

  if userInput == "y":
    print("Here we go! ")
    print()
  elif userInput == "n":
    print("Too bad we're starting anyways")
  else:
    print("Wrong Input Try Again")

在try块中,您没有引发错误,因此没有要捕获的内容。增加工资就可以了

userInput = input("Are both players ready to start? (y/n): ")
try:
  if userInput == "y":
    print("Here we go! ")
    print()
  elif userInput == "n":
    print("Too bad we're starting anyways")
  else:
    raise ValueError("What's up with that?")
except:
  print("Wrong Input Try Again")
按照@ctrl-alt-delor的建议,您也可以跳过
try/except
块,只使用
if/else
块。此代码段应执行相同的操作:

  if userInput == "y":
    print("Here we go! ")
    print()
  elif userInput == "n":
    print("Too bad we're starting anyways")
  else:
    print("Wrong Input Try Again")

如果用户没有添加任何输入,则不会引发异常,只是您的两个条件都不是真的。然后没有外部循环使其再次询问。缺少一个字符串引号,它应该读为
print(“错误输入重试”)
。这可能不是您的答案,只需使用if、elif和else(不要使用Try,except)。如果用户不添加任何输入,则不会引发异常,只是您的两个条件都不正确。然后没有外部循环使其再次询问。缺少一个字符串引号,它应该读为
print(“错误输入重试”)
。这可能不是你的答案,只需使用if、elif、else(不要使用Try,除非)。它给了我一个错误:未定义原始输入。我想这是因为这是Python3.7Ok,别提那部分,我刚刚意识到我使用的是Python2.7。我将编辑Python2的answerraw_输入,现在是Python3的输入,而您的代码将正常工作。既然你可以在else中处理输入错误,为什么还要抛出并捕获异常?@ChamongLo
if/elif/else
也会这样做!它给了我一个错误:未定义原始输入。我想这是因为这是Python3.7Ok,别提那部分,我刚刚意识到我使用的是Python2.7。我将编辑Python2的answerraw_输入,现在是Python3的输入,而您的代码将正常工作。既然你可以在else中处理输入错误,为什么还要抛出并捕获异常?@ChamongLo
if/elif/else
也会这样做!