我想用python在系統中進行日誌記錄。要登錄,您必須先註冊。註冊後,將其存儲到帶有base64密碼的文本文件中。然後,如果您登錄,它會要求輸入用戶名和密碼,並將密碼編碼到base64中,並根據文本文檔進行檢查。但是當它搜索密碼時什麼也沒有發生。它只是結束程序並進入shell。沒有回溯錯誤或任何東西。就好像其餘的代碼是不可見的。 代碼如下:Python程序剛剛結束循環
try:
import linecache
import base64
import tkinter
import time
choice = ""
def choose():
choice = input("Would you liek to register or login: ")
choice = choice.upper()
if choice == "REGISTER":
f = open("h.txt", "a")
f.write("\n")
print("Please enetr a username")
username = input(">>> ")
print("Please enter a password")
passw = input(">>> ")
print("\n"*100)
passw_conf = input("Please enter your password again\n>>> ")
print("\n"*100)
if passw == passw_conf:
print("Thank you for Registering")
f.write(username + "\n")
passw = base64.b64encode(passw.encode('utf-8'))
passw = str(passw)[2:-1]
f.write(passw + "\n")
f.close()
choose()
else:
print("Please choose register again as passwords do not match.")
elif choice == "LOGIN":
username = input("Please enter your username\n>>> ")
passw = input("Please enter your password\n>>> ")
x = 0
with open("h.txt") as f:
lines = [line.rstrip('\n') for line in open('h.txt')]
while lines[x] != username:
print(lines[x])
if lines == username:
print("Got it ;)")
elif lines != username:
print("Not got it ;(")
passw = base64.b64encode(passw.encode('utf-8'))
passw = str(passw)[2:-1]
if passw == lines:
print("Logged in successfully")
elif lines == "":
print("Username not found!")
choose()
except KeyboardInterrupt:
print("Restarting....")
time.sleep(8)
print("\n"*100)
choose()
choose()
你可以嘗試設置斷點流量變量(使用'進口PDB的每一行代碼後的一個; pdb.set_trace() ')並逐步瀏覽代碼以查看程序終止的位置? – MatanRubin
不應該在while循環中添加'x + = 1'嗎?你也應該測試'lines [x] == username',而不是'lines',因爲這只是所有行的列表 – Martinbaste