【问题标题】:How to avoid Repeated input by the user [duplicate]如何避免用户重复输入[重复]
【发布时间】:2021-06-06 11:38:55
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。将输入添加到集合中,然后检查该集合是否已包含当前输入。

标签: python input while-loop


【解决方案1】:

我不知道这是否有帮助,但你可以试试这个:

given_inputs = []

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 2017-02-12
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    相关资源
    最近更新 更多