2016-12-07 87 views
0

我想用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() 
+0

你可以嘗試設置斷點流量變量(使用'進口PDB的每一行代碼後的一個; pdb.set_trace() ')並逐步瀏覽代碼以查看程序終止的位置? – MatanRubin

+0

不應該在while循環中添加'x + = 1'嗎?你也應該測試'lines [x] == username',而不是'lines',因爲這只是所有行的列表 – Martinbaste

回答

4

代碼有很多問題。只有在h.txt中輸入的第一個用戶名是輸入的用戶名,否則你只能使用無限循環。其次,lines是一個列表,因此永遠不會等於passwd""。因此沒有打印。

它應該是這樣的:

username = input("Please enter your username\n>>> ") 
    passw = input("Please enter your password\n>>> ") 

    with open("h.txt") as f: 
     lines = [line.rstrip('\n') for line in f] 
    for line in lines: 
     if line == username: 
      user_password = next(lines) 
      break 
    else: 
     print("Username not found!") 
     return 
    passw = base64.b64encode(passw.encode('utf-8')) 
    passw = passw[2:-1] 
    if passw == user_password: 
     print("Logged in successfully") 
    else: 
     print("Wrong password") 
1

執行一次,我們預計第一行應該是用戶名,這意味着,在while處斷裂x=0(同時啓動)。 然後變量lines是一個列表,它不等於密碼,它不是空的(沒有打印)。對不起,程序執行你寫的東西!

,我看到其他的東西:

  • 你兩次打開文件(用語句+以下線)
  • while循環是不是循環(X + = 1)
  • 如果語句來對while語句後永遠是真實的(if lines[x] == username不能爲lines[x] != username工作)

有調試這一個簡單的方法:打印!

  • 顯示您正在使用,以控制(lines