Мне нужно использовать функцию login (), затем функцию gameswon (), а затем функцию входа в систему. При первом использовании входа в систему переменная player должна == 1, затем функция gameswon установит player == 2 для системы входа в систему. Но при втором использовании login () он все еще думает, что player == 1.
Я пробовал использовать глобальный проигрыватель, Айв также пытался использовать внешний файл, ни один из них не работал.
Функция входа:
def login():
global player
player="1"
loop="incorrect"
global p1username
global p2username
if player=="1":
p1username=0
p2username=1
if player=="2":
p2username=1
username=0
password=0
open("dice_scores.txt","a")
file=open("users.txt","a")
while loop==("incorrect"):
accq=input("Player "+player+" do you have an account? ")
if accq=="ADMIN":
admin=open("users.txt","r")
filecontents = admin.read()
print(filecontents)
if accq=="yes":
open("users.txt")
userbase=open("users.txt")
username=input("Input username: ")
password=input("Input password: ")
if player==str(1):
p1username=username
elif player==str(2):
p2username=username
combinedup=(username+"."+password+",")
with open("users.txt") as x:
if username==1 or username==0:
print("Username cannot be 1 or 0")
loop=("incorrect")
if p2username==p1username:
print("User already logged in, please try again.")
loop=("incorrect")
else:
if combinedup in x.read():
loop=("correct")
print("Correct Details","\n")
else:
print("Incorrect Details")
loop=("incorrect")
if accq=="no":
userbase=open("users.txt","a")
accexist="exists"
while accexist=="exists":
createusername=input("Create username: ")
createpassword=input("Create password: ")
combinedup=(createusername+"."+createpassword+",")
with open("users.txt") as x:
if createusername+"." in x.read():
print("Username already exists try again")
accexist=("exists")
else:
accexist=("valid")
userbase.write(combinedup + "\n")
print("Account created successfully! Return to login")
loop=("incorrect")
userbase.close()
accq=0
username=0
password=0
loop="incorrect"
Функция Gameswon:
global player
global p1gameswon
global p1gameslost
global p2gameswon
global p2gameslost
p1gameswon=0
p1gameslost=0
p2gameswon=0
p2gameslost=0
file=open("dice_scores.txt","r")
if player=="2":
for gameswon in file:
if (p2username) in gameswon and ("GAME WON!") in gameswon:
p2gameswon=p2gameswon+1
file=open("dice_scores.txt","r")
for gameslost in file:
if (p2username) in gameslost and ("GAME LOST!") in gameslost:
p2gameslost=p2gameslost+1
print("Welcome",p2username,"you have won",p2gameswon,"games and lost",p2gameslost,"games!","\n")
if player=="1":
for gameswon in file:
if (p1username) in gameswon and ("GAME WON!") in gameswon:
p1gameswon=p1gameswon+1
file=open("dice_scores.txt","r")
for gameslost in file:
if (p1username) in gameslost and ("GAME LOST!") in gameslost:
p1gameslost=p1gameslost+1
print("Welcome",p1username,"you have won",p1gameswon,"games and lost",p1gameslost,"games!","\n")
playerfile=open("player.txt","w")
player=playerfile.write(str("2"))
player="2"
print("DONE")
file.close()