我正在尝试登录/注册系统。现在唯一的问题是,如何在主代码中全局访问变量和字符串?我知道给他们下定义,不是个好主意。但我试过这样做,但我确实得到了变量的错误,当我这样做的时候:
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))
我的问题是如何使用这些函数作为全局函数,我可以使用它们而无需定义? 密码,用户名,平衡,规则时间,角色,禁止
您正在尝试声明与函数同名的全局变量。这就是获得输出
<function Ballance at 0x7f6f0662bc80>
的原因—您正在打印名为Ballance
的函数您需要将全局变量重命名为其他变量,或者更好的是,使用class将函数作为方法收集,使用属性而不是全局变量。您可以尝试以下方法:
你可以这样使用这个类:
您可以添加代码来初始化
__init__
方法中的属性(可以使用像0.00
这样的默认值,也可以使用input()
提示用户)。然后,除了get_
方法之外,还可以添加方法来改变状态,如下所示:听起来你会从Python类或面向对象编程的教程中受益匪浅。您应该确保了解上面的
self
和__init__
方法所做的工作相关问题 更多 >
编程相关推荐