Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序在循环后结束_Python_Python 3.x - Fatal编程技术网

Python程序在循环后结束

Python程序在循环后结束,python,python-3.x,Python,Python 3.x,我正在尝试用python制作一个登录系统。要登录,您必须先注册。注册后,它将以base64中的密码存储到文本文件中。然后,如果您登录,它会要求输入用户名和密码,密码将被编码到base64中,并根据文本文档进行检查。但是当它搜索密码时,什么也没有发生。它只是结束程序并转到shell。没有回溯错误或任何东西。这就好像代码的其余部分是不可见的。 代码如下: try: import linecache import base64 import tkinter import

我正在尝试用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()

你的代码有很多问题。只有当
h.txt
中的第一个用户名是输入的用户名时,它才起作用,否则您将有一个无限循环。其次,
是一个列表,因此永远不能等于
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")

你的代码有很多问题。只有当
h.txt
中的第一个用户名是输入的用户名时,它才起作用,否则您将有一个无限循环。其次,
是一个列表,因此永远不能等于
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")

执行一次,我们希望第一行应该是username,这意味着
while
x=0
处中断(whilestart)。 然后变量
lines
是一个列表,它不等于密码,也不为空(无打印)。抱歉,程序执行了您编写的操作

我看到的其他事情:

  • 您正在打开文件两次(下面有语句+行)
  • while循环不是循环(x+=1)
  • while语句后面的if语句永远不能为true(
    if行[x]==username
    不能用于
    lines[x]!=username
有一种简单的调试方法:print

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

执行一次,我们希望第一行应该是username,这意味着
while
x=0
(启动时)中断。 然后变量
lines
是一个列表,它不等于密码,也不为空(无打印)。抱歉,程序执行了您编写的操作

我看到的其他事情:

  • 您正在打开文件两次(下面有语句+行)
  • while循环不是循环(x+=1)
  • while语句后面的if语句永远不能为true(
    if行[x]==username
    不能用于
    lines[x]!=username
有一种简单的调试方法:print

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

是否尝试设置断点(使用
import pdb;pdb.set_trace()
)并单步执行代码以查看程序终止的位置?是否应在while循环中添加
x+=1
?此外,您还应该测试
行[x]==用户名
,而不是
,因为这只是一个包含所有行的列表。您可以尝试设置一个断点(使用
import pdb;pdb.set_trace()
)并单步执行代码以查看程序终止的位置。是否应该在while循环中添加
x+=1
?您还应该测试
行[x]==username
,而不是
,因为这只是一个包含所有行的列表