我在Python中有一个分配,其中用户从嵌套列表中查找密码。我得到的大部分代码都只需要完成即可。我拥有的代码足以让我上交作业,但是我想对我做错了什么进行解释。如果我选择列表中的第二个“ google”
我已经阅读了有关嵌套列表和循环的所有内容,但找不到所需的内容。我已经取出嵌套的for循环,这只会使情况更糟。我知道这只是我对如何执行此操作的不了解。
print("Which website do you want to lookup the password for?")
for keyvalue in passwords:
print(keyvalue[0])
passwordToLookup = input()
for i in passwords:
if passwordToLookup in i:
print("Your encrypted password is: " + i[1])
print("Your unencrypted password is: " + passwordEncrypt(i[1], -16))
break
else:
print("Login does not exist.")
我只希望它查找用户输入的内容,即“ google”。输出底部的第三行表示“登录不存在”,它表示“ yahoo”,不要以为这应该在这里,但不确定。只是需要一些解释或指示。
What would you like to do:
1. Open password file
2. Lookup a password
3. Add a password
4. Save password file
5. Print the encrypted password list (for testing)
6. Quit program
Please enter a number (1-4)
2
Which website do you want to lookup the password for?
yahoo
google
google
Login does not exist.
Your encrypted password is: CoIushujSetu
Your unencrypted password is: MySecretCode
谢谢!
答案 0 :(得分:0)
打印数据库与您的问题无关。
发生的事情是,如果您看下面的代码:
for i in passwords:
if passwordToLookup in i:
print("Your encrypted password is: " + i[1])
print("Your unencrypted password is: " + passwordEncrypt(i[1], -16))
break
else:
print("Login does not exist.")
您可以看到它将打印Login does not exist.
直到找到为止。要解决此问题,您将需要使用布尔值,并等到循环完成后再运行print("Login does not exist.")
。因此,它看起来像这样:
doesNotExist = True
for i in passwords:
if passwordToLookup in i:
print("Your encrypted password is: " + i[1])
print("Your unencrypted password is: " + passwordEncrypt(i[1], -16))
doesNotExist = False
break
if doesNotExist:
print ("Login does not exist.")