python - 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: "))
我不想使用任何其他库。
解决方案
你result
是一个字符串列表,你将输入作为整数,这就是为什么playersID
永远不会在result
. 您需要将输入行更改为以下内容:
改变
playersID = int(input("Enter ID of the player: "))
至
playersID = input("Enter ID of the player: ")
推荐阅读
- database-design - 我在 ER 图中只在一个中创建了两个关系。我可以这样做吗?
- python - 自动生成的名称是字母而不是数字
- html - 如何使用引导 html 数据切换切换多个数据目标和切换
- kubernetes - 如何删除作为 kubectl attach/exec 目标的 Pod 或重新启动容器?
- sql - Oracle正则表达式用字母替换数字
- c# - Windows 服务的 C# 代码在执行 ExecuteReader() 期间抛出异常
- angular - Angular 中的 ng-component 是什么?
- javascript - 如何在具有键值对的对象数组中查找值
- delphi - ListView 从第二个 TListItem.Caption 重复项目
- python - 如何在 numpy 中实现定点二进制支持