簡體   English   中英

Python:如何檢查用戶輸入的整數是否與列表中的項目不匹配?

[英]python: how to check the integer input by user match the item in the list of not?

我有一個場景。 我有一個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: ")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM