在python中使用多个变量创建列表是if语句的一部分

2024-10-03 11:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

   def runUser():
       x = raw_input("Enter your username:")
       y = raw_input("Enter your password :")
       users = [x == "aeduun" and y == "1234", x == "paul" and y == "fifty"]
       raw_input("")
       if x == users[] and y == users[]:#x == "aeduun" and y == "1234":
           print "you are now logged in"
       elif x == "Mercuryisle" and y == "shrek":
           print "Your account has expired..." \
                 "You will now bw taken back to the login page"
           time.sleep(5)
       return runUser()

下面是我作为python2.7小型测试项目的一部分编译的代码。在本文中,我试图用密码和用户名的变量列表来伪造一个基本登录名。该函数能够读取变量之间的关系,但包含对不同列表项的引用的错误。 当我启动程序时,只会调用一个无效的用户名(代码不显示)。 我想要一些关于如何初始化列表项的索引的建议,这些索引将作为if语句的一部分读取。你知道吗


Tags: and代码列表inputyourrawifdef
2条回答
def runUser():
   x = raw_input("Enter your username: ")
   y = raw_input("Enter your password: ")
   users = {"aeduun":"1234", "paul":"fifty"}
   raw_input("")
   if x in users:
       if y == users[x]:
           print "you are now logged in"
   elif x == "Mercuryisle" and y == "shrek":
       print "Your account has expired..." \
             "You will now bw taken back to the login page"
       time.sleep(5)
   return runUser()

运行方式:

>>> runUser()
Enter your username: aeduun
Enter your password: 1234

you are now logged in
Enter your username: Mercuryisle
Enter your password: shrek

Your account has expired...You will now bw taken back to the login page
Enter your username: 

还有一个建议,使用getpass.getpass()来获取密码,这样可以防止字符回音。你知道吗

以明文形式存储密码是不安全的,但是考虑到这一点,我认为使用元组和集合(即

users = set([("aeduun","1234"), ("paul","fifty")])
expired_users = set([("Mercuryisle", "shrek")])
. . .
if (x,y) in users:
    print "you are now logged in
elif (x,y) in expired_users:
    print "Your account has expired..."
. . .

相关问题 更多 >