我正在嘗試在 Python 中創建密碼管理器,但出現值錯誤。下面是我為密碼管理器撰寫的代碼。它使用密碼輸入。
def view():
with open("passwords.txt", "r") as f:
for line in f.readline():
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name "|" pwd "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
這是我在運行和輸入輸入時遇到的錯誤:
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 22, in <module>
view()
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 8, in view
user, passw = data.split("|")
ValueError: not enough values to unpack (expected 2, got 1)
有人可以幫助我并指導我如何解決此錯誤嗎?
謝謝你。
uj5u.com熱心網友回復:
def view():
with open("passwords.txt", "r") as f:
for line in f:
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name "|" pwd "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
背景關系:您試圖讀取該行的每個字符而不是讀取每一行,因此在嘗試拆分時它會給您一個錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458920.html
下一篇:在JAR檔案中找不到檔案
