如何制作字符串,TXT文本的变量在主代码中为全局



我正在尝试进行登录/寄存器系统。现在唯一的问题是,如何在主代码中访问变量和字符串?我知道定义它们不是一个好主意。但是我已经尝试这样做,当我这样做时,我确实会遇到错误的错误:

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 accountn")
                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))

我的问题是如何将这些函数用作全局,我可以在没有定义的情况下使用它们?密码用户名 ballance regtime roun /strong>。

您正在尝试声明具有与函数相同名称的全局变量。这就是为什么您获得输出<function Ballance at 0x7f6f0662bc80>-您正在打印称为Ballance的函数。

您需要将全局变量重命名为其他事物,或者更好,或者更好地使用类将功能作为方法收集,而不是属性而不是全局变量。您可以尝试以下内容:

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__方法。

最新更新