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 :)