我正在尝试替换列表中的一个项目,不一定在末尾,只是替换原始项目的位置,然后让它在要求您输入密码时需要新项目。我对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
如果知道列表中元素的值(假设要更改F Block的密码,} 函数返回其索引,如下所示:
'mrjoefblock'
),可以使用^{这样你就可以更新密码并保持相同的位置(如果这是你所要求的)。你知道吗
但这样修改密码对我来说似乎很奇怪。我不会把它列成一张单子,而是一本字典:
我们现在可以找到具有给定密码的所有用户:
并且很容易更改任何用户的密码:
抱歉,一开始不清楚你在问什么。你知道吗
我举了一个简单的例子:
下面是它的工作原理:
这样,如果需要,您可以继续向用户询问密码,否则继续。你知道吗
要替换列表中的项目,只需按其列表位置进行分配:
相关问题 更多 >
编程相关推荐