Python 如何验证多个用户满足其输入的要求?

Python 如何验证多个用户满足其输入的要求?,python,python-3.x,Python,Python 3.x,我正在为无限数量的玩家制作一个游戏。每个玩家将猜出1到25之间的5个数字。(还有更多的要求,您将在代码中看到)我的问题是,我希望第一个用户输入他的猜测,如果他的猜测满足了要求,我们将返回并遍历下一个玩家猜测的名称列表。问题是我不知道如何打破循环,回到“主”循环,找到下一个玩家。感谢您对这个问题的建议 while True: try: for name in name_list: guess = input("{}, Ente

我正在为无限数量的玩家制作一个游戏。每个玩家将猜出1到25之间的5个数字。(还有更多的要求,您将在代码中看到)我的问题是,我希望第一个用户输入他的猜测,如果他的猜测满足了要求,我们将返回并遍历下一个玩家猜测的名称列表。问题是我不知道如何打破循环,回到“主”循环,找到下一个玩家。感谢您对这个问题的建议

while True:
        try:
            for name in name_list:
                guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                guess = guess.split(",")
                guess = [int(i) for i in guess]

                while True:
                    for i in guess:
                        if len(guess) != 5:
                            print("You need to enter 5 numbers")
                            guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                            guess = guess.split(",")
                            guess = [int(i) for i in guess]

                        elif guess.count(i) > 1:
                            print("There can only be one of each number!")
                            guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                            guess = guess.split(",")
                            guess = [int(i) for i in guess]

                        elif i > 25 or i < 1:
                            print("The numbers need to be between 1 and 25")
                            guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                            guess = guess.split(",")
                            guess = [int(i) for i in guess]
                else:
                    break


            guess_list.append(guess)
            print("Your numbers: " + str(guess) + "\n")


        except ValueError:
            print("You need to enter integers and seperate by (',')")
            continue
´´´
为True时:
尝试:
对于名称列表中的名称:
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
尽管如此:
因为我想:
如果len(猜)!=5:
打印(“您需要输入5个数字”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
elif guess.count(i)>1:
打印(“每个数字只能有一个!”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
如果i>25或i<1:
打印(“数字需要介于1和25之间”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
其他:
打破
猜测列表。追加(猜测)
打印(“您的数字:”+str(猜测)+“\n”)
除值错误外:
打印(“您需要输入整数并用(','))分隔)
持续
´´´

我已经阅读了您的代码,其中有几个问题。我已经对它们做了一个快速的总结,以及(我认为)你正在尝试做的事情的正确版本

在循环中检查
len(guess)
时,应在检查每个元素之前检查长度一次

对于您最初的问题,请查看
input\u good
变量,该变量在for循环中设置为true,如果有任何错误,则设置为False。。。这个变量将让内部while循环知道在一切正常时自行中断(因为我们一次只能直接中断一个循环)

这是一个起点,接下来您应该考虑使用函数来处理重复行为,并将
try/except
放在尽可能少的代码中(错误处理中使用catch anywhere是一种糟糕的做法)

#使其运行的名称列表
name_list=['Matthew','Luke','Mark','John']
尽管如此:
尝试:
对于名称列表中的名称:
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
尽管如此:
#该if位于整个列表中,因此不应位于枚举中
如果len(猜)!=5:
打印(“您需要输入5个数字”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
#继续将导致while循环再次运行,
#如果statemtn使用新的猜测数再次运行,则会导致此错误
持续
因为我想:
#我们现在需要打破2个循环。。。
#因此,我们设置了一个标志,外部循环将检查并中断输入是否良好
输入良好=正确
如果猜数(i)>1:
打印(“每个数字只能有一个!”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
输入良好=错误
#输入good False表示外部循环不会中断,
#但我们仍然希望打破for循环,回到while循环
打破
如果i>25或i<1:
打印(“数字需要介于1和25之间”)
guess=input(“{},输入5个介于1和25之间的数字(以“,”分隔):”。格式(名称))
猜测=猜测。拆分(“,”)
guess=[int(i)表示我在猜测]
输入良好=错误
中断#如上所述
打印('运行%s'%str(输入良好))
如果输入良好:
打破
#此else中断不起任何作用,因为它仅在循环条件为false且True始终为True时被命中
#其他:
#中断
猜测列表。追加(猜测)
打印(“您的数字:”+str(猜测)+“\n”)
除值错误外:
打印(“您需要输入整数并用(','))分隔)
持续

break
仅在调用最内部的循环时中断。如果要中断多个循环(如
for
循环和内部
while
循环),则需要将变量设置为true,如果为true,则在两个循环中调用break。请参阅
# list of names so it runs
name_list = ['Matthew', 'Luke', 'Mark', 'John']

while True:
    try:
        for name in name_list:
            guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
            guess = guess.split(",")
            guess = [int(i) for i in guess]

            while True:
                # This if is on the whole list, so it shouldn't be inside the enumeration
                if len(guess) != 5:
                    print("You need to enter 5 numbers")
                    guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                    guess = guess.split(",")
                    guess = [int(i) for i in guess]
                    # continue will cause the while loop to run again,
                    # causing this if statemtn to run again with the new guess numbers
                    continue

                for i in guess:
                    # We need to break out of 2 loops now...
                    # So we set a flag that the outer loop will check and break if the input is good
                    input_good = True
                    if guess.count(i) > 1:
                        print("There can only be one of each number!")
                        guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                        guess = guess.split(",")
                        guess = [int(i) for i in guess]
                        input_good = False
                        # input good False means the outer loop won't break,
                        # but we still want to break out of the for loop and back into the while loop
                        break

                    elif i > 25 or i < 1:
                        print("The numbers need to be between 1 and 25")
                        guess = input("{}, Enter 5 numbers between 1 and 25 (Seperate with ','): ".format(name))
                        guess = guess.split(",")
                        guess = [int(i) for i in guess]
                        input_good = False
                        break # as above
                print('run %s' % str(input_good))
                if input_good:
                    break
            # This else break does nothing as it is only hit when the loop condition is false and while True is always true
            #else:
            #    break
        guess_list.append(guess)
        print("Your numbers: " + str(guess) + "\n")


    except ValueError:
        print("You need to enter integers and seperate by (',')")
        continue