适用于所有部分的一个部分的条件

时间:2012-12-31 16:55:54

标签: python python-2.5

我已多次尝试修复此问题,但我仍然无法找出问题所在。这个想法是给每个“用户”一个不同的“密码”,让他们访问我将构建的其他东西,或者如果密码错误,则结束程序。

但是,每当我尝试为用户分配不同于顶级用户的密码时(如下面的情况),它仍然认为用户1的PW是其他所有用户的密码。

那是什么造成了这种情况,我该如何解决?

print "Enter your name."

answer = raw_input()

if answer == "User 1":
    print "Hello.Please enter your passcode."

    answer = raw_input()

    if answer == "orion":
        print "Login request accepted. Welcome to The Database."
    elif answer != "orion":
        print "LOGIN DENIED. Your intrusion has been reported to the authorities."

elif answer == "User 2":
    print "Hello.Please enter your passcode."

    answer = raw_input()

    if answer == "bandit":
        print "Login request accepted. Welcome to The Database."
    elif answer != "bandit":
        print "LOGIN DENIED. Your intrusion has been reported to the authorities."

1 个答案:

答案 0 :(得分:1)

两个例子

我举两个例子说明如何做到这一点(毕竟是新年的一天):)

程序代码

您的代码有效,但您可能想尝试一下,更清洁一点:

users = {"User 1" : { 'pwd' : 'bandit', 'role': 'admin'},
         "User 2" : { 'pwd' : 'orion', 'role': 'user'},
         "User 3" : { 'pwd' : 'solar', 'role': 'approver'},
         "User 4" : { 'pwd' : 'mars', 'role': 'editor'}
         }

print "Enter your name."
answer = raw_input()

INTRUSION = True

if answer in users:
    print "Hello %s. Please enter your passcode." % answer
    password = raw_input()
    if password == users[answer]['pwd']:
        INTRUSION = False

if INTRUSION:
    print "LOGIN DENIED. Your intrusion has been reported to the authorities."
else:
    print "Login request accepted. Welcome to The Database."
    print "Your role is %s." % users[answer]['role']
    # ...you can now go on to do other stuff

它允许您引用用户词典并为其分配不同的角色。您可能会发现在此版本上展开更容易。

面向对象的示例

更广泛的示例使用更面向对象的方法:

from collections import namedtuple
import hashlib

# ---------------------------- SETUP 

# data structure for holding user record in named fields
User = namedtuple('User', ['uid', 'pwd', 'role'])

# utility function to hash passwords and ensure they are
# stored in encrypted form
def hashed(s):
    return hashlib.md5(s).hexdigest()

# Class to hold the user database
# Methods could be added here e.g. add and delete users
class UserBase(object):
    def __init__(self, users={}):
        self.database = {}
        for user in users:
            self.database[user.uid] = user

    def get_user(self, uid):
        return self.database.get(uid)

# These should be stored in a database with
# hashed passwords...
users = [User("User 1", hashed('bandit'), 'admin'),
         User("User 2", hashed('orion'), 'user'),
         User("User 3", hashed('solar'), 'approver'),
         User("User 4", hashed('mars'), 'editor')]

# Instantiate the database
USERS = UserBase(users)

# ---------------------------- SETUP ENDED

# Simulate a login session

INTRUSION = True
uid = raw_input("Enter your name: ")

user = USERS.get_user(uid)
if user:
    prompt = "Hello %s. Please enter your passcode: " % user.uid
    password = raw_input(prompt)
    if hashed(password) == user.pwd:
        INTRUSION = False

if INTRUSION:
    print "LOGIN DENIED. Your intrusion has been reported to the authorities."
else:
    print "%s logged in. Welcome to The Database." % user.uid
    print "Your role is %s." % user.role
    # ...you can now go on to do other stuff
    # there is an object called user that has
    # three attributes: uid, pwd, and role