使用循环&amp ;;比较列表中的项目条件陈述

时间:2017-02-18 21:24:42

标签: python-2.7

我试图遍历一个列表并将这些项目与一个人输入的内容进行比较,然后做出决定。我的逻辑出错的任何想法?

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")

1 个答案:

答案 0 :(得分: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"