首页 > 解决方案 > python:如何检查用户输入的整数是否匹配列表中的项目?

问题描述

我有一个场景。我有一个 ID 类似 result = ['003234568', '000000001', '123456789'] 的列表。

我想要求用户输入 ID 并循环直到 ID 是唯一的。我尝试了这个,但没有得到预期的输出。

result = ['003234568', '000000001', '123456789'].
playersID = int(input ("Enter ID of the player: "))
while playersID in result:
    print ("players id already exist, please enter new one")
    playersID = int(input ("Enter ID of the player: "))

我不想使用任何其他库。

标签: pythonpython-3.x

解决方案


result是一个字符串列表,你将输入作为整数,这就是为什么playersID永远不会在result. 您需要将输入行更改为以下内容:

改变

playersID = int(input("Enter ID of the player: "))

playersID = input("Enter ID of the player: ")

推荐阅读