jxc
jxc

Reputation: 11

Looking to properly append scores to an existing file

Finishing off my program and I want to append the results of "playerUserNumber", "playerComputerNumber" and "gameDraw" to an existing file as follows (example):
User 1
Computer 2
Draws 3
But it is appending as follows: playerUserNumber, playerComputerNumber, gameDraw

Here is the full code of the program:

#Asks for name, validation which doesn't accept numbers.
while True:
userName = input("Enter your name: ") 
if not userName.isalpha():
    print("Not valid\n")
else:
    break

#Prints out the rules to the user
rules = ["\nEach player rolls two dice. If the scores on the two dice are different, they are added. If they are the same then the score is increased be 50% (so two 3s would score a 9).",
"If one of the scores is greater than 12 the other player automatically wins.",
"If one of the scores is equal to twelve, that player wins.",
"If both scores are greater than 12 or both equal to 12, it is a draw.",
"If both scores are equal and less than twelve, they both throw again.",
"If both scores are unequal and less than 12 then the person with the higher score has the choice to either stick with their score, or throw one further dice.",
"If they throw again and all three dice are the same number (eg in the first two throws they scored two 2s and in the third throw they score another 2) then they automatically win with a triple.",                                                                                
"Otherwise they add the new score to the old one, after which the other player throws one further dice. If they have all three dice scores the same, they win with a triple.",
"Otherwise:", 
"If both total scores are less than or equal to 12:",
"(a)it is a draw if the scores are equal or",
"(b)the player with the highest score wins",
"if one player has a total score greater than twelve they lose, if both have scores higher 
 than twelve it is a draw.\n"]
 
 #While loop
 while True:
     #Asks question
     rulesQuestion = input("\nDo you know the rules? Type Yes or No: ")
     #If awnser is no then prints out rules
     if rulesQuestion == "No" or rulesQuestion == "no":
         for rule in rules: print(rule)
         break
     #If awnser is yes then it will just continue
     elif rulesQuestion =="Yes" or rulesQuestion == "yes":
         break
     else:
         #Asks user to enter yes or no
         print("Please enter either Yes/No: ")

def dice():
    #Imports the random module
    import random

    #Game scoring system
    playerUserNumber = 0
    playerComputerNumber = 0
    gameDraw = 0

    #Number between 1-6
    playerUserDiceFirst = random.randint(1, 6)
    playerUserDiceSecond = random.randint(1,6)
    playerComputerDiceFirst = random.randint(1, 6)
    playerComputerDiceSecond = random.randint(1,6)

    #Adds the 2 scores
    playerUserTotal = playerUserDiceFirst + playerUserDiceSecond
    playerComputerTotal = playerComputerDiceFirst + playerComputerDiceSecond

    #Output texts
    print(userName, "has scored: ", playerUserDiceFirst)
    print(userName, "has scored: ", playerUserDiceSecond)
    print(userName, "total score is: ", playerUserTotal)
    print("\nComputer has scored: ", playerComputerDiceFirst)
    print("Computer has score: ", playerComputerDiceSecond)
    print("Computer total score is: ", playerComputerTotal)

    #If rolled the same, increase by 50%
    userScoredTheSame = int( playerUserTotal / 0.50)
    computerScoredTheSame = int(playerComputerTotal / 0.50)

    #If both dice are the same then increase by 50%
    if playerUserDiceFirst == playerUserDiceSecond:
        print("Since", userName, "scored the same number, you're new score is: ", userScoredTheSame)
    #If both dice are the same then increase by 50%
    elif playerComputerDiceFirst == playerComputerDiceSecond:
            print("Since the computer scored the same number, its new score is: ", computerScoredTheSame)
    #If both dice are equal to 12 and greater to 12 then it's a draw
    elif playerUserTotal == playerComputerTotal > 12:
            print("Since both score is equal to 12, greater than 12 then it's a draw ")
            gameDraw =+1

    #If computers total lower than users' then user win and adds a point to game score
    if playerUserTotal > playerComputerTotal:
        print("\nCongratulations",userName, "you have won the game!")
        playerUserNumber +=1
    #If users total lower than computers' then computer win and adds a point to game score    
    elif playerComputerTotal > playerUserTotal:
        print("\nCongratulations computer has won the game!")
        playerComputerNumber +=1
    #If both draw then adds a point to draw game score
    elif playerUserTotal == playerComputerTotal:
        print("It's a draw")
        gameDraw +=1
dice()

#While loop
while True:
    #Asks if user wants to play again
    playAgainQuestion = input("\nDo you want to play again? ").lower()
    #If yes, repeat dice 
    if playAgainQuestion == "Yes" or playAgainQuestion == "yes":
        dice()
    else:
        #If no then appends the scores to game file
        with open ("GameFile.txt", "a") as file:
            file.write("playerUserNumber, playerComputerNumber, gameDraw")
        break

If it's okay with you guys/girls, if you find anything wrong with the way I've coded the program can you show the better/correct way (ideally not advanced but more beginner/novice friendly) and kindly explain it to me.

Thanks, jxc

Upvotes: 0

Views: 82

Answers (1)

Ethan Thompson
Ethan Thompson

Reputation: 56

In the second last line

file.write("playerUserNumber, playerComputerNumber, gameDraw")

You need to use \n to go to the next line. Instead, try

file.write("playerUserNumber\nplayerComputerNumber\ngameDraw")

This would then append to the file in the format

playerUserNumber
playerComputerNumber
gameDraw

As for writing the actual values of those variables try replacing the line in question with

file.write("%i\n%i\n%i" % (playerUserNumber, playerComputerNumber, gameDraw))

This is a nice way of printing integer values :)

Upvotes: 1

Related Questions