How do I save scores in an external text file from a rock, paper, scissors, game?

0

Issue

I am very new to python so I apologise for that. anyway I have programmed a simple rock, paper, scissors game where you play against a bot that randomly pulls "rock", "paper", or "scissors" from a list. The game (if you can call it that) works fine, However, I want to be able to save the game score to a text file, preferably called "score.txt", when the game ends, I have already tried doing this and it failed miserably, Heres the output from "score.txt"

Test |  | # theres meant to be a "Score" and a Win, Loss, and Tie in between
Test |  | # the | |, for e.g, Test | Win | 5 - Score
Test |  | #                   Test | Win | 10
Test |  | #                   Test | Loss | 5

and heres the code for the program I made:

import random
rps = ["rock","paper","scissors"]
score = "score"
score1 = 0
name = None
i = None
f = open("score.txt")

while not name:
    name = str(input("Enter a name: "))
for i in range(4):
    while not (i == "rock" or i == "paper" or i == "scissors"):
        i = str(input("Enter rock paper or scissors: "))
    choice_rps = random.choice(rps)
    print(choice_rps)

    if i == "rock" and choice_rps == "paper":
        result = "lost"
        print("You lost :(")
    elif i == "paper" and choice_rps == "rock":
        result = "won"
        print("You won!!!")
    elif i == "paper" and choice_rps == "scissors":
        result = "lost"
        print("You lost :(")
    elif i == "scissors" and choice_rps == "paper":
        result = "won"
        print("You won!!!!!!!")
    elif i == "scissors" and choice_rps == "rock":
        result = "lost"
        print("You lost :(")
    elif i == "rock" and choice_rps == "scissors":
        result = "won"
        print("You won!!!!!!")
    else:
        result = "tie"
        print("It's a tie")

        if result == "won":
            print("score + 5")
            score1 = score1 + 5
            f.write("\n" + name + " | Win | " + str(score1))
        elif result == "lost":
            print("score - 5")
            score1 = score1 - 5
            f.write("\n" + name + " | Loss | " + str(score1))
        elif result == "tie":
            print("score stays same")
            score1 = score1

Apologises if this seemms confusing to read. Any advice would be greatly appreciated.
(I’m ten years old so more towards a simple answer would be appreciated 🙂

Solution

There’s three issues with your code:

  1. open() takes in a second parameter that describes whether we want to read, write, or append to a file. In this case, we want to write to the file, so we should use open('score.txt', 'w') — the w is for write.
  2. You need to close your file after you’re done writing to it using f.close().
  3. The code to actually write to the file needs to be shifted over one indent. Otherwise, if we win, the score handling code will never be executed (since it’s under an else branch that handles ties).

Here’s the code with all three fixes:

import random
rps = ["rock","paper","scissors"]
score = "score"
score1 = 0
name = None
i = None
f = open("score.txt", "w")

while not name:
    name = str(input("Enter a name: "))

for i in range(4):
    while not (i == "rock" or i == "paper" or i == "scissors"):
        i = str(input("Enter rock paper or scissors: "))
    choice_rps = random.choice(rps)
    print(choice_rps)

    if i == "rock" and choice_rps == "paper":
        result = "lost"
        print("You lost :(")
    elif i == "paper" and choice_rps == "rock":
        result = "won"
        print("You won!!!")
    elif i == "paper" and choice_rps == "scissors":
        result = "lost"
        print("You lost :(")
    elif i == "scissors" and choice_rps == "paper":
        result = "won"
        print("You won!!!!!!!")
    elif i == "scissors" and choice_rps == "rock":
        result = "lost"
        print("You lost :(")
    elif i == "rock" and choice_rps == "scissors":
        result = "won"
        print("You won!!!!!!")
    else:
        result = "tie"
        print("It's a tie")

    if result == "won":
        print("score + 5")
        score1 = score1 + 5
        f.write("\n" + name + " | Win | " + str(score1))
    elif result == "lost":
        print("score - 5")
        score1 = score1 - 5
        f.write("\n" + name + " | Loss | " + str(score1))
    elif result == "tie":
        print("score stays same")
        score1 = score1

f.close()

Answered By – BrokenBenchmark

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More