在if语句中检查范围的问题

哈里·拉福

我是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: ")```
ppwater

说明

那是因为首先,如果输入不是y,那么它将说无法识别。这是因为,如果它是y,它将检查no语句,然后它会中断。这意味着,它将同时说两个。

然后,这变得很奇怪,因为您正在使用str和str做> =。它会做出奇怪的反应。因此,您应该将int与int进行比较。

首先,在第一个语句中使用和。然后,使用elif。并在if语句中也使用break。并做int(input())转换为int。然后,您可以再次使用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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章