Python 3.x 检查if语句中的范围时出现问题

Python 3.x 检查if语句中的范围时出现问题,python-3.x,Python 3.x,我是python新手,正在做网络游戏。当然,其中一个模块是Python 我正在尝试编写一个没有教程的数字猜谜游戏。有人能看看这段代码并告诉我为什么代码似乎没有遵循if语句吗 谢谢 编辑:错误:我正在尝试创建代码,通过检查玩家所选择的难度范围来判断玩家所选择的难度。问题是,它正在打印每个难度选项,而不仅仅是它适合的那个 编辑2:将标题更改为更具体,以便帮助他人 print("Welcome to the number guesser game!") tutorial = i

我是python新手,正在做网络游戏。当然,其中一个模块是Python

我正在尝试编写一个没有教程的数字猜谜游戏。有人能看看这段代码并告诉我为什么代码似乎没有遵循if语句吗

谢谢

编辑:错误:我正在尝试创建代码,通过检查玩家所选择的难度范围来判断玩家所选择的难度。问题是,它正在打印每个难度选项,而不仅仅是它适合的那个

编辑2:将标题更改为更具体,以便帮助他人


print("Welcome to the number guesser game!")

tutorial = input("Would you like a tutorial? (Y/N): ")

while tutorial != "Y" or "y" or "N" or "n":
      input("Input not recognised, would you like a tutorial? Y (for yes) or N (for no)")
      if tutorial == "N" or "n":
         print("Let's get right into it!")
         if tutorial == "Y" or "y":
            print("In this game you will pick a number, the number you pick will be added to a random number between 1 and 20 /nYou will then attempt to guess the number generated, with a hint given every guess")
            break


difficulty = input("Please choose a random number \nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1,000\nExtreme Numbers above 1,000-10,000")


if difficulty >="1" <="10":
   print("You have chosen easy! You must be a noob!")
   if difficulty >"10" <="100":
      print("You have chosen medium! Good choice!")
      if difficulty >"100" <="1000":
         print("You have chosen hard! Pfft, good luck")
         if difficulty >"1000" <="10000":
            print("You have chosen extreme! You must be nuts")
            if difficulty <="0" >"10000":
               difficulty = input("Nice try, pick again: ")
            else:
                 difficulty = input("Input not recognised, please input a number greater than 0: ")```
解释 这是因为首先,如果输入不是y,那么它会说notrecognized。 这是因为,如果它是y,它检查no语句,然后它就中断了。 也就是说,两者都有

然后,它会变得奇怪,因为您正在使用str和str执行>=操作。它的反应会很奇怪。所以你应该比较int和int

解决方案 首先,在第一个语句中使用and。然后,使用elif。在if语句中也使用break。然后,您可以再次使用elif。并删除引号

试试这个:

print("Welcome to the number guesser game!")

tutorial = input("Would you like a tutorial? (Y/N): ")
while True:
      if tutorial != "Y" and tutorial !="y" and tutorial !="N" and tutorial !="n":
          tutorial = input("Input not recognised, would you like a tutorial? Y (for yes) or N (for no)")
      if tutorial == "N" or tutorial =="n":
         print("Let's get right into it!")
         break
      elif tutorial == "Y" or tutorial == "y":
         print("In this game you will pick a number, the number you pick will be added to a random number between 1 and 20 /nYou will then attempt to guess the number generated, with a hint given every guess")
         break


difficulty = int(input("Please choose a random number \nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1,000\nExtreme Numbers above 1,000-10,000"))


if difficulty >=1 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
elif difficulty >10 and difficulty<=100:
    print("You have chosen medium! Good choice!")
elif difficulty >100 and difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
elif difficulty >1000 and difficulty<=10000:
    print("You have chosen extreme! You must be nuts")
elif difficulty <=0 and difficulty >10000:
    difficulty = input("Nice try, pick again: ")
else:
    difficulty = input("Input not recognised, please input a number greater than 0: ")
对于while循环,将其设置为while True:然后使用if语句检查它。然后您可以检查y或n。

说明 这是因为首先,如果输入不是y,那么它会说notrecognized。 这是因为,如果它是y,它检查no语句,然后它就中断了。 也就是说,两者都有

然后,它会变得奇怪,因为您正在使用str和str执行>=操作。它的反应会很奇怪。所以你应该比较int和int

解决方案 首先,在第一个语句中使用and。然后,使用elif。在if语句中也使用break。然后,您可以再次使用elif。并删除引号

试试这个:

print("Welcome to the number guesser game!")

tutorial = input("Would you like a tutorial? (Y/N): ")
while True:
      if tutorial != "Y" and tutorial !="y" and tutorial !="N" and tutorial !="n":
          tutorial = input("Input not recognised, would you like a tutorial? Y (for yes) or N (for no)")
      if tutorial == "N" or tutorial =="n":
         print("Let's get right into it!")
         break
      elif tutorial == "Y" or tutorial == "y":
         print("In this game you will pick a number, the number you pick will be added to a random number between 1 and 20 /nYou will then attempt to guess the number generated, with a hint given every guess")
         break


difficulty = int(input("Please choose a random number \nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1,000\nExtreme Numbers above 1,000-10,000"))


if difficulty >=1 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
elif difficulty >10 and difficulty<=100:
    print("You have chosen medium! Good choice!")
elif difficulty >100 and difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
elif difficulty >1000 and difficulty<=10000:
    print("You have chosen extreme! You must be nuts")
elif difficulty <=0 and difficulty >10000:
    difficulty = input("Nice try, pick again: ")
else:
    difficulty = input("Input not recognised, please input a number greater than 0: ")

对于while循环,将其设置为while True:然后使用if语句检查它。然后可以检查y或n。

首先在while循环中检查变量tutorial是y还是y或n或n您正在执行tutorial!=Y或Y或N或N,这是不正确的,因为它表示如果教程!=Y或Y或n或n,后3个条件返回true,因为它是非空字符串


对于if条件,有3个问题,第一个是我刚才描述的问题,第二个是在检查之前没有将输入转换为整数,第三个是不必要地嵌套条件。

首先在while循环中检查变量tutorial是Y还是Y或n还是n您正在执行tutorial!=Y或Y或N或N,这是不正确的,因为它表示如果教程!=Y或Y或n或n,后3个条件返回true,因为它是非空字符串


对于if条件,有3个问题,第一个是我刚才描述的问题,第二个是在检查之前没有将输入转换为整数,第三个是不必要地嵌套了条件。

简化检查,并在循环中执行,以便在输入无效输入时无法继续:

difficulty = -1
while difficulty < 1 or difficulty > 10000

  try
    difficulty = int(input("Please choose a difficulty.\nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1000\nExtreme 1000-10000\n: "))
  except ValueError:
    print("not a number")

  if difficulty > 0 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
  elif difficulty<=100:
    print("You have chosen medium! Good choice!")
  elif difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
  elif difficulty<=10000:
    print("You have chosen extreme! You must be nuts")

您不需要每个elif来确保该数字大于之前检查范围的末尾;如果不是这样的话,埃利夫会是真的,而不是这一个。换句话说,如果你和另一个人玩猜谜游戏,你说数字小于等于10,他们说不,你不需要说它大于10小于。。因为大于是隐含的-它必须大于,因为它不小于或等于简化您的检查,并在循环中执行,以便在输入无效输入时无法继续:

difficulty = -1
while difficulty < 1 or difficulty > 10000

  try
    difficulty = int(input("Please choose a difficulty.\nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1000\nExtreme 1000-10000\n: "))
  except ValueError:
    print("not a number")

  if difficulty > 0 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
  elif difficulty<=100:
    print("You have chosen medium! Good choice!")
  elif difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
  elif difficulty<=10000:
    print("You have chosen extreme! You must be nuts")

您不需要每个elif来确保该数字大于之前检查范围的末尾;如果不是这样的话,埃利夫会是真的,而不是这一个。换句话说,如果你和另一个人玩猜谜游戏,你说数字小于等于10,他们说不,你不需要说它大于10小于。。因为大于是隐含的-它必须大于,因为它不小于或等于

你想做什么?嗨,阿文。。我试图让if语句打印出玩家是否在列出的数字之间选择了一个值,然后打印他们选择的难度。但是当我运行这个代码时,它会打印出所有的困难。你能分享一下关于为什么你认为你的代码不起作用的信息吗?有什么错误吗?输出不正确?或者到底是什么?另外,如果有错误,请编辑您的问题。@TanishqVyas editted@HarryRaffo请检查我的答案。您想做什么?嗨,Aven。。我试图让if语句打印出玩家是否在列出的数字之间选择了一个值,然后打印他们选择的难度。但是当我运行这个代码时,它会打印出所有的困难。你能分享一下关于为什么你认为你的代码不起作用的信息吗?有什么错误吗?不正确的

t输出?或者到底是什么?另外,如果有错误,请编辑您的问题。@TanishqVyas editted@HarryRaffo请检查我的答案。这很有帮助,谢谢。向上投票,你可以给我一个简短的说明为什么我的代码不起作用。谢谢大家!@哈瑞拉福哦,好的。我会编辑的。@HarryRaffo编辑!谢谢你提醒我。Y N中的教程可能是检查input@HarryRaffo编辑。这很有帮助,谢谢你。向上投票,你可以给我一个简短的说明为什么我的代码不起作用。谢谢大家!@哈瑞拉福哦,好的。我会编辑的。@HarryRaffo编辑!谢谢你提醒我。Y N中的教程可能是检查input@HarryRaffo编辑。谢谢你的建议!非常感谢。谢谢你的建议!非常感谢。我正准备编辑并询问此事,感谢您的远见和帮助!您好,如果他们输入了无效选项,我如何编辑此代码以允许显示错误消息?您可以在末尾添加一个else,但这不是必需的;最初输入一个介于1到10000之间的数字的指令,如果他们不遵守就会被重复。是的,这很简单,可以理解,但有点不知所措哈哈。非常感谢。注意,我修复了while条件中的一个bug,并添加了一个处理非数字输入的尝试。我正要编辑并询问这个问题,感谢您的远见和帮助!您好,如果他们输入了无效选项,我如何编辑此代码以允许显示错误消息?您可以在末尾添加一个else,但这不是必需的;最初输入一个介于1到10000之间的数字的指令,如果他们不遵守就会被重复。是的,这很简单,可以理解,但有点不知所措哈哈。非常感谢。注意,我修复了while条件中的一个bug,并添加了一个尝试来处理非数字输入