奇怪的函数调用和不一致的结果

时间:2021-02-17 00:36:01

标签: python json python-3.x authentication pycharm

首先,我只想说我是一个新手,我为不好的解释和长篇文章道歉......

因此,作为实践,我编写了一个简单的 Python 登录系统,其中包含存储配置文件的 JSON 文件。

一切都很顺利,但突然间我的代码开始出现异常。 这是我的 main.py 文件:

import json

with open("profiles.json") as f:
    profiles = json.load(f)


def main():
    print("-----------------Main--------------------")
    option = input("[L]ogin | [S]ign up: ").upper()

    if option == "L":
        login()
    elif option == "S":
        sign_up()
    else:
        print("Please select a valid option.")
        main()


def login():
    print("-----------------Login--------------------")

    username = input("Username: ")
    password = input("Password: ")
    check_credentials(username, password)


def sign_up():
    print("-----------------Sign up--------------------")

    new_username = None
    new_password = None

    # check if this username already exists, return to sign up if true
    def username_match():
        nonlocal new_username

        new_username = input("Username: ")

        for profile in profiles["profiles"]:
            if new_username == profile["username"]:
                print("This username is taken.")
                username_match()

    # loop back if the passwords do not match
    def password_match():
        nonlocal new_password

        new_password = input("Password: ")
        confirm_password = input("Confirm Password: ")

        if new_password != confirm_password:
            print("Passwords do not match.")
            password_match()

    username_match()
    password_match()

    security_question = input("Security Question: ")
    security_answer = input("Security Question Answer: ")

    profiles["profiles"].append({"username": new_username,
                                 "password": new_password,
                                 "security_question": security_question,
                                 "security_answer": security_answer})

    with open("profiles.json", "w") as w:
        json.dump(profiles, w, indent=2)

    check_credentials(new_username, new_password)


def profile_settings():
    input("-----------------Options--------------------"
          "\n"
          "[P] change password | [U] change username"
          "\n"
          "[S] change security question | [E] add email"
          "\n"
          "What would you like to do: ").upper()

    print("\nThis section is under construction. Please visit later.")


def check_credentials(username, password):
    print("\nchecking credentials...\n")

    for profile in profiles["profiles"]:

        if profile["username"] != username and profile["password"] != password:
            print("Wrong username and password, please try again.")
            login()

        if profile["username"] == username:
            print(f"found username: {username}")
            if profile["password"] == password:
                print(f"found password: {password}")
            else:
                print("Wrong password, please try again.")
                login()
        else:
            print("Wrong username, please try again.")
            login()

        profile_settings()


main()

这是我的profiles.json文件:

{
  "profiles": [
    {
      "username": "Hakobu",
      "password": "123",
      "security_question": "favorite food",
      "security_answer": "lemon"
    },
    {
      "username": "Mohammed",
      "password": "345",
      "security_question": "1",
      "security_answer": "1"
    }
  ]
}

这是我发现的奇怪之处:<​​/p>

  1. 当我尝试登录第二个配置文件时,它告诉我凭据错误并使我返回到 login() 函数,但它让我进入第一个配置文件。
  2. 当尝试通过 sign_up() 函数创建新配置文件时,它应该自动登录但超出第一个配置文件,创建的第二个配置文件只是做同样的事情,它告诉我,错误的凭据并放入我回到 login() 函数。
  3. 使用第一个配置文件成功登录后,将调用 profile_settings() 函数。它应该在输入任何内容后关闭,但它返回到 check_credentials() 函数,说我输入了错误的用户名和密码,然后在 login() 函数之后直接转到 profile_settings() 函数即使我没有在 profile_settings() 函数中的任何地方调用它们

我不知道为什么会发生这种情况。就在不久前,它运行良好。尝试注释掉我在工作后编写的代码,但没有任何效果。我现在头疼得厉害,背也疼。

1 个答案:

答案 0 :(得分:0)

在了解堆栈调用和堆栈帧之后,我现在知道问题只是在退出 import { notification } from "antd"; const openNotificationWithIcon = (type: string, message: string, err: string) => { if(type == 'success' || type == 'warning') notification[type]({ message: message, description: err}); return; }; export default openNotificationWithIcon; 后恢复了 for 循环,导致该函数似乎无限循环。

这是改进后的代码:

check_credentials()