2017-02-18 39 views
0

我試圖循環查看列表並比較項目與某人輸入的內容,然後做出決定。任何想法,我的邏輯出錯了?使用循環和條件語句比較列表中的項目

user = input("Enter your password: ") 
passwords = ["pa$$w0rd", "password123", "scr1pt1ng", "F0r3n51c5", "123456"]; 

for n in passwords: 
    if n == user: 
     print("Found", n) 
     break 
    else: 
     print(user, "Not Found") 
+0

在Python 2.7這是'打印用戶, 「未找到」'。但是:它工作嗎?你有錯誤嗎?請告訴我們:)([mcve]) –

回答

1

您需要詳細瞭解python2和python3的區別。

在你的代碼中你使用python3樣式:print(something),因爲python2樣式應該是print "something"; input VS raw_input

所以,你的版本是Python3

我不使用python2,但它應該是這樣的:

user = raw_input("Enter your password: ") # input changed to raw_input 
passwords = ["pa$$w0rd", "password123", "scr1pt1ng", "F0r3n51c5", "123456"]; 

for n in passwords: 
    if n == user: 
     print "Found", n 
     break 
    else: 
     print user, "Not Found" 
+0

感謝您對TitanFighter的反饋,非常感謝:) – Joe