Reputation: 53
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 :)
Upvotes: 2
Views: 567
Reputation: 520
Edits in the code:
1- with open("score.txt", "a") as f:
# not to use close func, "a" is to append new values, use "w" if you want to rewrite the whole file
2- indentation fixed
3- using string format allows you to print a dynamic string
here is a working version of your code: import random
rps = ["rock", "paper", "scissors"]
score = "score"
score1 = 0
name = None
i = None
with open("score.txt", "a") as f: # not to use close func, "a" is to append new values, use "w" if you want to rewrite the whole file
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": #indentation fixed from here to the end
print("score + 5")
score1 = score1 + 5
f.write(f"{name} | Win | {str(score1)} \n")#using string format allows you to have a dynamic string
elif result == "lost":
print("score - 5")
score1 = score1 - 5
f.write(f"{name} | Lose | {str(score1)} \n")
elif result == "tie":
print("score stays same")
score1 = score1
Upvotes: 0
Reputation: 19242
There's three issues with your code:
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.f.close()
.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()
Upvotes: 4