Python 试图用另一项替换列表中的项

Python 试图用另一项替换列表中的项,python,Python,我试图替换列表中的一个项目,不一定在最后,只是替换原始项目的位置,然后让它在询问您的密码时需要新项目。我对Python有点陌生 passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrjoeadmin' ] if choice == '3': password = raw_input('Welcome admin! I\'m going to need your password ') if p

我试图替换列表中的一个项目,不一定在最后,只是替换原始项目的位置,然后让它在询问您的密码时需要新项目。我对Python有点陌生

passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrjoeadmin' ]
if choice == '3':
        password = raw_input('Welcome admin! I\'m going to need your password ')
        if password == 'mrjoeadmin':
            print('Welcome Mr. Joe!')
            Choice11 = raw_input('What would you like to do? Press 1 for changing your admin password, 2 for viewing a class\'s comments, or 3 for changing a class\'s password')
            if Choice11 == '1':
                print('You have chosen to change your password! ')
                Choice12 = raw_input('You will need to put in your current password to access this feature ')
                if Choice12 == 'mrjoeadmin':
                    Choice13 = raw_input('What would you like to change your password to? ')
                    passwords.remove('mrjoeadmin')
                    passwords.append(Choice13)
                    print('Thank you! Password has been changed. Log in with new password next time')
                    break
            if Choice11 == '2':
                print('you have chosen to view a class\'s comments!')
                Choice14 = raw_input('Which block would you like to view?')
                if Choice14 == 'F block':
                    print('To view F block\'s comments, go to where you search for things in your computer, and search CommentsFblock.txt. Open it')
                    time.sleep(10)
                    break
                if Choice14 == 'G block':
                    print('To view G block\'s comments, go to where you search for things in your computer, and search CommentsGblock.txt. Open it')
                    time.sleep(10)
                    break
                if Choice14 == 'B block':
                    print('To view B block\'s comments, go to where you search for things in your computer, and search CommentsBblock.txt. Open it')
                    time.sleep(10)
                    break
                else:
                    print('I\'m sorry, that\'s not a valid block name.')
                    time.sleep(2)
                    break
            if Choice11 == '3':
                print('you have chosen to change a block\'s password.')
                Choice15 = raw_input('Which block\'s password would you like to change?')
                if Choice15 == 'F block':
                    Choice16 = raw_input('You will need to put in your current password to change F block\'s password ')
                    if Choice16 == 'mrjoeadmin':
                        Choice17 = raw_input('What would you like to change F block\'s password to? ')
                        passwords.remove('mrjoefblock')
                        passwords.append(Choice17)
                        print('Thank you! Password has been changed. Remember to tell students the password has been changed')
                        time.sleep(3)
                        break
                if Choice15 == 'G block':
                    Choice18 = raw_input('You will need to put in your current password to change G block\'s password ')
                    if Choice18 == 'mrjoeadmin':
                        Choice19 = raw_input('What would you like to change G block\'s password to? ')
                        passwords.remove('mrjoegblock')
                        passwords.append(Choice19)
                        print('Thank you! Password has been changed. Remember to tell students the password has been changed')
                        time.sleep(3)
                        break
                if Choice15 == 'B block':
                    Choice20 = raw_input('You will need to put in your current password to change B block\'s password ')
                    if Choice20 == 'mrjoeadmin':
                        Choice21 = raw_input('What would you like to change B block\'s password to? ')
                        passwords.remove('mrjoebblock')
                        passwords.append(Choice21)
                        print('Thank you! Password has been changed. Remember to tell students the password has been changed')
                        time.sleep(3)
                        break

要替换列表中的项目,只需按其列表位置进行分配:

mylist = ['hello', 'goodbye', 'goodnight']

mylist[2] = 'good day'

如果您知道列表中元素的值(假设您要更改F Block的密码,
'mrjoefblock'
),则可以使用函数返回其索引,如下所示:

In [1]: passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrjoeadmin' ]

In [2]: passwords.index('mrjoefblock')
Out[2]: 1

In [3]: passwords[passwords.index('mrjoefblock')] = 'newjoefblock'

In [4]: passwords
Out[4]: ['mrjoebblock', 'newjoefblock', 'mrjoegblock', 'mrjoeadmin']
这样,您就可以更新密码并保持在相同的位置(如果这是您所要求的)

但对我来说,这样更改密码似乎很奇怪。我不想把它列成一个清单,而是一本字典:

In [1]: credentials = {
   ...:     'B Block': 'mrjoebblock',
   ...:     'F Block': 'mrjoefblock',
   ...:     'G Block': 'mrjoegblock',
   ...:     'Admin': 'mrjoeadmin'
   ...: }
我们现在可以找到具有给定密码的所有用户:

In [2]: [user for user, password in credentials.iteritems() if password == 'mrjoeadmin']
Out[2]: ['Admin']
并可轻松更改任何用户的密码:

In [3]: credentials['F Block'] = 'newjoefblock'

In [4]: credentials
Out[4]:
{'Admin': 'mrjoeadmin',
 'B Block': 'mrjoebblock',
 'F Block': 'newjoefblock',
 'G Block': 'mrjoegblock'}

对不起,最初不清楚你在问什么

我举了一个简单的例子:

def login():
    """
    This function asks for the password and returns either the username (if logged in successfully)
    or False (if the password was incorrect).
    """
    password = raw_input("Password: ")

    if password == 'admin':
        return 'Administrator'
    elif password == 'ggguest':
        return 'Guest'
    else:
        return False


if __name__ == '__main__':
    # We'll introduce a new variable which will be either a logged in user's name
    # or False (based on login()'s function result)
    user_logged_in = login()

    while True:  # An endless loop
        if user_logged_in:
            # the login() function didn't return False, so we're logged in!
            print "Yay! You're now logged in as {0}!".format(user_logged_in)

            # Here we'll do whatever you need to. 
            # I kept the example simple so it just asks if the user wants to log out. 
            # You can change the user's password here and ask for the password again.

            want_to_logout = raw_input("Want to log out? [y/n]: ")

            if want_to_logout == 'y':
                user_logged_in = login()  # Ask for the password again
            else:
                print 'Alright then! Do your thing!'
                break  # Break out of loop and continue
        else:
            # wrong password, ask again
            print "Wrong password :("
            user_logged_in = login()  # Ask for the password again

    print "Keep up the good job, {0}!".format(user_logged_in)
下面是它的工作原理:

$ python admin.py
Password: wrong_password
Wrong password :(
Password: admin
Yay! You're now logged in as Administrator!
Want to log out? [y/n]: y
Password: ggguest
Yay! You're now logged in as Guest!
Want to log out? [y/n]: n
Alright then! Do your thing!
Keep up the good job, Guest!

这样,如果需要,您可以继续向用户询问密码,否则继续。

那么,您有什么问题吗?我正试图让它在您更改密码后需要新密码it@KraeganEpsilon明白了。我更新了我的答案。谢谢,但是我如何使它在下次用户尝试登录时需要密码?例如,如果password=passwords[4],会是这样吗?