Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 没有';对于';循环并将检查器从False更改为True?_Python_Python 3.x - Fatal编程技术网

Python 没有';对于';循环并将检查器从False更改为True?

Python 没有';对于';循环并将检查器从False更改为True?,python,python-3.x,Python,Python 3.x,对python非常陌生,作为练习,我一直在做一个基于文本的棋盘游戏(我是一个狂热的棋盘游戏玩家) 显然,用户有很多地方可以输入一些东西,当然我需要评估这些输入并确保它们是有效的 我从这个网站学到了使用真/假检查和for循环(谢谢!),现在我已经做了这么多了,我想知道是否有更好的方法——如果它们是嵌套的,它们会变得有点疯狂,我希望尽我所能遵守那些类似禅宗的Python规则 下面是我的意思,在一个简单的例子中,使用通用代码,这样才有意义(不是所有人都知道这个棋盘游戏的规则!) 目标是确保代码循环直到

对python非常陌生,作为练习,我一直在做一个基于文本的棋盘游戏(我是一个狂热的棋盘游戏玩家)

显然,用户有很多地方可以输入一些东西,当然我需要评估这些输入并确保它们是有效的

我从这个网站学到了使用真/假检查和for循环(谢谢!),现在我已经做了这么多了,我想知道是否有更好的方法——如果它们是嵌套的,它们会变得有点疯狂,我希望尽我所能遵守那些类似禅宗的Python规则

下面是我的意思,在一个简单的例子中,使用通用代码,这样才有意义(不是所有人都知道这个棋盘游戏的规则!)

目标是确保代码循环直到给出颜色或“无”。我知道这可以实现这一点,我只是想知道是否有一种更精简的方式我还没有学会

colors = ["red", "green", "white", "blue", "yellow"]

for color in colors:
    print(f"You can choose {color}.")

choice = input("Which color would you choose? (input a color or 'none' : ")
checker = False

while not checker:
    if choice not in colors:
        choice = input("That's not one of your options. Choose again: ")

    elif choice == "none":
        print("You don't want a color. That's fine.")
        checker = True

    else:
        print("You chose {color}! Have fun!")
        checker = True

如果您发现自己在重复相同的事情,那么可以定义一个泛型函数

def prompt_for_valid_input(options):
    while True:
        print(f"You can choose one of {options}.")
        choice = input("Which would you choose? (or 'none' : ")
        if choice == "none" and "none" not in options:
            print("You didn't pick anything. That's fine.")
            return None
        elif choice in options:
            return choice
        else:
            print("That's not one of your options. Choose again. ")

colors = ["red", "green", "white", "blue", "yellow"]
color = prompt_for_valid_input(colors)
if color is not None:
    print(f"You chose {color}! Have fun!")

numbers = [1, 10, 100]
num = prompt_for_valid_input(numbers)
if num is not None:
    print(f"You chose {num}! Have fun!")

所以“没有一段时间/循环”,不是真的。如果条件足够简单,没有sentinel变量,是的

您可以将其抽象为一个经过验证的通用输入法

def validated_input(prompt,validation_method,error_message):
    while True:
         result = input(prompt)
         if validation_method(result):
            return result
         print(error_message)


choices = ['red','green','blue','none']
validator = lambda test:test.lower().strip() in choices
prompt = "Enter A Color(one of Red, Green, Blue, or None):"
error_message = "Please enter a valid color or None!"

print(validated_input(prompt,validator,error_message))

这是否“更好”还有争议

通常,您编写代码的方式被认为是非常“正常”和简单的,这很好。问题是,您希望代码的循环部分重复,直到达到预期。因此,有条件循环事件是必要的。如果您确实需要,您可以通过做一个中断语句来简化它,例如:

x = 0
while True:
  if x == 12:
     break;
  else:
     x = x + 1
print(x)

这将创建一个无限循环,直到满足您要查找的条件,然后它将中断循环。但要确保你有逃生条件

这看起来很合理……考虑一下像
argparse
click
这样的成熟参数解析器——它们不需要在程序的命令行参数上使用。即使你实际上没有使用其中的一个,这种心态可能是有用的。谢谢你,我喜欢这个想法。在我的代码中,经常输入某个输入会启动一个函数,该函数本身可能有更多的输入验证器。我喜欢这个想法,可能会在正确的地点明智地实施。谢谢乔丹。我现在明白了,这样的函数实际上可以帮我很多忙。我的验证通常会产生一个函数,有时这些函数会有更多的验证。我将使用这些构造,看看是否可以改进代码!