Python 如何避免用户重复输入

Python 如何避免用户重复输入,python,input,while-loop,Python,Input,While Loop,如何确保相同的输入不会在python中的while循环中重复 解释-我想检查用户是否已经给出了相同的输入,如果是,则告诉它相同的信息。我不知道这是否有帮助,但您可以尝试以下方法: given_inputs = [] while True: x = input('>') if x not in given_inputs: given_inputs.append(x) print('New Input!') else:

如何确保相同的输入不会在python中的while循环中重复
解释-我想检查用户是否已经给出了相同的输入,如果是,则告诉它相同的信息。

我不知道这是否有帮助,但您可以尝试以下方法:

given_inputs = []

while True:
    x = input('>')
    if x not in given_inputs:
        given_inputs.append(x)
        print('New Input!')
    else:
        print('Input not valid')

欢迎来到堆栈溢出。将输入添加到集合中,然后检查集合是否已包含当前输入。