如何使字符串、变量从TXT文本中成为全局的主代码

2024-10-03 23:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试登录/注册系统。现在唯一的问题是,如何在主代码中全局访问变量和字符串?我知道给他们下定义,不是个好主意。但我试过这样做,但我确实得到了变量的错误,当我这样做的时候:

def Ballance(Ballance):
    global Ballance
    Ballance = 0.00
    return Ballance

并尝试在这里使用:

print(" Ballance {} psw {} Your Ballance {} EUR ".format(Vardas, Password, Ballance))

我确实在候机楼收到这个

Ballance Jut psw jut Your Ballance <function Ballance at 0x7f6f0662bc80> EUR 

我的全部代码:

# Text File.
Database = 'Registruoti.txt'
check = True

def Vardas():
    global Vardas
    Vardas = input("~ Please pick a username for you Account!\n")
    return Vardas

def Password():
    global Password
    Password = getpass.getpass("~ Create a password for your account {}\n".format(Vardas))
    return Password

def Ballance(Ballance):
    global Ballance
    Ballance = 0.00
    return Ballance

def Role():
    global Role
    Role = 'Member'
    return Role

def Ban():
    global Ban
    Ban = False
    return Ban

def RegTime():
    global RegTime
    RegTime = strftime("%Y-%m-%d %H:%M", gmtime())
    return RegTime

while check:
    Register_Login = input("~ Welcome, LOGIN L, REGISTER R.\n")
    if "r" in Register_Login or "R" in Register_Login:
        with open(Database, mode='a', encoding='utf-8') as f:
            Vardas()
            Password()
            #Vardas = input("~ Please pick a username for you Account!\n")
            #Password = getpass.getpass("~ Create a password for your account {}\n".format(Vardas))
            if " " in Vardas or " " in Password or len(Vardas) < 3 or len(Password) < 3 :
                print(" Cannot Contain null!")
                continue
            else:
                Gmail = input("~ Please add a Gmail for your account\n")
                if " " in Gmail or len(Gmail) < 7 :
                        print("Cannot Contain null!")
                        continue
                else:
                        # Setting up New account. Options Roles.
                        Ballance()
                        RegTime()
                        Ban()
                        Role()
                        f.write(f"Vardas : {Vardas} Password : {Password} Gmail: {Gmail} Ballance : {Ballance} BAN : {Ban} Role: {Role} RegTime : {RegTime}\n")
                        f.close()
                        break
    elif "l" in Register_Login or "L" in Register_Login:
        while check:
            with open(Database, mode = 'r', encoding = 'utf-8') as f:
                    Vardas = input("Please enter your Username!\n")
                    Password = getpass.getpass("Please enter your Password!\n")
                    for line in f:
                        if "Vardas : " + Vardas + " Password : " + Password + " " in line.strip():
                            print("You're logged in")
                            f.close()
                            check = False
                            break;
                        else:
                            clear()
                            print("Wrong password!")
                            check = True
                            continue;

print(" Ballance {} psw {} Your Ballance {} EUR ".format(Vardas, Password, Ballance))

我的问题是如何使用这些函数作为全局函数,我可以使用它们而无需定义? 密码用户名平衡规则时间角色禁止


Tags: orinforreturndefpasswordglobalgmail
1条回答
网友
1楼 · 发布于 2024-10-03 23:24:05

您正在尝试声明与函数同名的全局变量。这就是获得输出<function Ballance at 0x7f6f0662bc80>的原因—您正在打印名为Ballance的函数

您需要将全局变量重命名为其他变量,或者更好的是,使用class将函数作为方法收集,使用属性而不是全局变量。您可以尝试以下方法:

class BankAccount:
    def __init__(self):
        self._balance = 0.00
        ...  # more attributes

    def get_balance(self):
        return self._balance

    ...  # more methods

你可以这样使用这个类:

my_bank_account = BankAccount()

...

print("Your balance is {} EUR.".format(my_bank_account.get_balance()))

您可以添加代码来初始化__init__方法中的属性(可以使用像0.00这样的默认值,也可以使用input()提示用户)。然后,除了get_方法之外,还可以添加方法来改变状态,如下所示:

class BankAccount:
    ...
    def increase_balance(self, amount):
        self._balance += amount

    def decrease_balance(self, amount):
        self._balance -= amount
    ...

听起来你会从Python类或面向对象编程的教程中受益匪浅。您应该确保了解上面的self__init__方法所做的工作

相关问题 更多 >