Python程序在循环

时间:2016-12-07 08:50:35

标签: python python-3.x

我正在尝试使用python登录系统。要登录,您必须先注册。注册后,将其存储到文本文件中,密码为base64。然后,如果您登录它,则要求输入密码被编码到base64中的用户名和密码,并根据文本文档进行检查。但是当它搜索密码时没有任何反应。它只是结束程序并转到shell。没有回溯错误或任何事情。好像其余的代码是不可见的。 代码如下:

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()

2 个答案:

答案 0 :(得分:5)

您的代码存在许多问题。它仅适用于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 :(得分:1)

执行一次,我们希望第一行应该是用户名,这意味着,while会在x=0(while-start)处中断。 然后变量lines是一个列表,它不等于密码而且它不是空的(没有打印)。对不起,该程序完成了你所写的内容!

我看到的其他事情:

  • 您要打开文件两次(使用下面的语句+行)
  • while循环不是循环(x + = 1)
  • if语句之后的if语句永远不能为真(if lines[x] == username不能用于lines[x] != username

有一种简单的方法可以调试它:打印!

  • 每行代码后面一个
  • 显示您用来控制流量的变量(lines