Напишите результат, например, победитель, рядом с именем пользователя и паролем в файле пользователя. - PullRequest
0 голосов
/ 19 февраля 2020

Я пытался записать результат в файл различными способами, но в прошлый раз, когда я определял победителя и проигравшего сверху

    import random
    import sys
    winner = ("winner")
    loser = ("loser")
    def logIn():


        account = input("Do you already have an account y/n?")


        if account == "n":
            while True:
                username = input("Please create a username:")
                password = input("Please create a password:")
                password1 = input("Please confirm your password:")
                if password == password1:
                    file = open("users.txt", "a")
                    file.write(username+":"+password+"\n")
                    file.close()
                    print ("Account successfully created \(^o^)/\n")
                    account = "y"
                    break
                print("The passowrds do not match. Please try again:")


        if account == "y":
            while True:
                login1 = input("Please enter your username:")
                login2 = input("Please enter your password:")
                file = open("users.txt", "r")
                data = file.read()
                file.close()
                if login1+":"+login2 in data:
                    print ("You are successfully logged in",login1+".")
                    break
                print ("Your username or password is incorrect. Please try again")
    def writewinner():
        file = open("users.txt", "a")
        file.write(username+":"+password+":"+winner+"\n")
        file.close()
    def writeloser():
        file = open("users.txt", "a")
        file.write(username+":"+password+":"+loser+"\n")
        file.close()
    logIn()
    def startgame():
        coins = 100
        print("starting amount of coins:", coins)
        while coins < 250 and coins > 0:
            takeinput = input("guess heads or tails: ")
            howmanycoins = int(input("How many coins do you want to bet on this: "))
            result = random.randint(0,1)
            if(result == 0 and takeinput == "heads"):
                print("well done")
                coins = coins + howmanycoins
                print("amount of coins:", coins)
                exit_choice()
            elif(result == 1 and takeinput == "tails"):
                print("well done")
                coins = coins + howmanycoins
                print("amount of coins:", coins)
                exit_choice()
            else:
                print("unlucky")
                coins = coins - howmanycoins
                print("amount of coins:", coins)
                restart_choice()


            if coins >= 250:
                print("well done you won with this amount of coins", coins)
                restart_choice()
                writewinner()
            elif coins <= 0:
                print("unlucky you lost all your coins")
                restart_choice()
                writeloser()

Выше указано, где файл, в который записывается, называется

    def exit_choice():
        print("Do you want to: \n(C) Continue \n(Q)Quit")
        while True:
            userresponse = input(">>> ")
            if userresponse == "C" or userresponse == "Q":
                break
            else:
                print("You must choose C or Q")
        if(userresponse == "Q"):
            sys.exit()

    def restart_choice():
        print("Do you want to: \n(R) Restart \n (Q)Quit")
        while True:
            userresponse = input(">>> ")
            if userresponse == "R" or userresponse == "Q":
                break
            else:
                print("You must choose R or Q")
        if(userresponse == "R"):
            startgame()
        if(userresponse == "Q"):
            sys.exit()

    def start():
        print("Do you want to: \n(P) Play \n(Q)Quit")
        while True:
            userresponse = input(">>> ")
            if userresponse == "P" or userresponse == "Q":
                break
            else:
                print("You must choose P or Q")
        if(userresponse == "P"):
            startgame()
        if(userresponse == "Q"):
            sys.exit()  
    start()
...