Python pickle adds first double value instead of single
Question:
This is the version of rock paper scissors game but I dont seem to find the solution to why it always adds double values to the first score that you get. For example if I play two games in a row it prints out double the amount of the first score and single amount of the second score and stores both of them inside the pickle.
import pickle
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
CHOICES = '''
1 - Play
2 - Statistic
3 - Quit'''
STATISTIC = '''
1 - This game results
2 - History of all results
3 - Quit
'''
victory, defeat, draw = [0, 0, 0]
victory_all, defeat_all, draw_all = [0, 0, 0]
gestures = [rock, paper, scissors]
while True:
choice = input(CHOICES)
if choice == "1":
try:
with open("rezultatai46.pkl", "rb") as file_pickle:
victory_all = pickle.load(file_pickle)
defeat_all = pickle.load(file_pickle)
draw_all = pickle.load(file_pickle)
except FileNotFoundError:
with open("rezultatai46.pkl", "wb") as file_pickle:
storage = victory_all, defeat_all, draw_all
pickle.dumps(victory_all)
pickle.dumps(defeat_all)
pickle.dumps(draw_all)
print('nWelcome to ROCK PAPER SCISSORS game!n')
player_choice = int(input("Your choice:n0 - Rockn1 - Papern2 - Scissorsn"))
computer_choice = random.randint(0, 2)
if player_choice >= 3 or computer_choice < 0:
print("Character error, you lose!")
else:
print("Your choice:")
print(gestures[player_choice])
print("Computer choice:")
print(gestures[computer_choice])
if player_choice == 0 and computer_choice == 2:
victory += 1
print("Tu laimėjai!")
elif computer_choice == 0 and player_choice == 2:
defeat += 1
print("Tu pralaimėjai!")
elif computer_choice > player_choice:
computer_choice += 1
print("Tu pralaimėjai!")
elif player_choice > computer_choice:
victory += 1
print("Tu laimėjai!")
elif computer_choice == player_choice:
draw += 1
print("Lygiosios")
with open("rezultatai46.pkl", "wb") as file_pickle:
storage = victory_all, defeat_all, draw_all
victory_all = (victory_all + victory)
defeat_all = (defeat_all + defeat)
draw_all = (draw_all + draw)
pickle.dump(victory_all, file_pickle)
pickle.dump(defeat_all, file_pickle)
pickle.dump(draw_all, file_pickle)
elif choice == "2":
while True:
choice = input(STATISTIC)
if choice == "1":
print(f"This game session:nWon: {victory}n"
f"Lost: {defeat}nDraaw: {draw}")
elif choice == "2":
with open("rezultatai46.pkl", "rb") as file_pickle:
victory_all = pickle.load(file_pickle)
defeat_all = pickle.load(file_pickle)
draw_all = pickle.load(file_pickle)
print("Won in total:", victory_all)
print("Lost in total:", defeat_all)
print("Draw in total:", draw_all)
games_all = (victory_all + defeat_all + draw_all)
victory_percentage = ((victory_all / games_all) * 100)
print("Laimėjimai procentais: ", round(victory_percentage), "%")
elif choice == "3":
break
elif choice == "3":
print("Iki susitikimo!")
break
else:
print("Netinkamas pasirinkimas")
Answers:
First – you have an error in this part:
elif computer_choice > player_choice:
computer_choice += 1
print("Tu pralaimėjai!")
It should be adding to defeat
, not computer_choice
. That is the cause of seem different behaviors for wins and losses.
Second: you are counting the current score to variables victory, defeat, draw
, since program start, and always adding the total local score to the total scores when pickling – but then you update again the total score when starting the next round.
Bear with me:
round 1:
total_victory = 0
victory = 0
Player wins:
victory = 1, total_victory += victory -> 1 and it is saved
round 2 starts at:
victory = 1
total_victory = 1
Player wins:
victory = 2
total_victory += victory -> 3
Round 3 starts at:
victory = 2
total_victory = 3
And after another win goes to:
victory = 3
total_victory += victory -> 6
The logic of this program is nice, and it can be seem you are being creative as you learn – which is great. But the fact you did not follow more usual patterns make it hard to fix without re-writting a significant part of your code.
So, as I pointed the error, I will leave it up to you to think how to fix it.
Probably the easier way is always adding "1" to total_victory, etc… as you add "1" to "victory", and never try to add "total_victory + victory" (and counterparts).
This is the version of rock paper scissors game but I dont seem to find the solution to why it always adds double values to the first score that you get. For example if I play two games in a row it prints out double the amount of the first score and single amount of the second score and stores both of them inside the pickle.
import pickle
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
CHOICES = '''
1 - Play
2 - Statistic
3 - Quit'''
STATISTIC = '''
1 - This game results
2 - History of all results
3 - Quit
'''
victory, defeat, draw = [0, 0, 0]
victory_all, defeat_all, draw_all = [0, 0, 0]
gestures = [rock, paper, scissors]
while True:
choice = input(CHOICES)
if choice == "1":
try:
with open("rezultatai46.pkl", "rb") as file_pickle:
victory_all = pickle.load(file_pickle)
defeat_all = pickle.load(file_pickle)
draw_all = pickle.load(file_pickle)
except FileNotFoundError:
with open("rezultatai46.pkl", "wb") as file_pickle:
storage = victory_all, defeat_all, draw_all
pickle.dumps(victory_all)
pickle.dumps(defeat_all)
pickle.dumps(draw_all)
print('nWelcome to ROCK PAPER SCISSORS game!n')
player_choice = int(input("Your choice:n0 - Rockn1 - Papern2 - Scissorsn"))
computer_choice = random.randint(0, 2)
if player_choice >= 3 or computer_choice < 0:
print("Character error, you lose!")
else:
print("Your choice:")
print(gestures[player_choice])
print("Computer choice:")
print(gestures[computer_choice])
if player_choice == 0 and computer_choice == 2:
victory += 1
print("Tu laimėjai!")
elif computer_choice == 0 and player_choice == 2:
defeat += 1
print("Tu pralaimėjai!")
elif computer_choice > player_choice:
computer_choice += 1
print("Tu pralaimėjai!")
elif player_choice > computer_choice:
victory += 1
print("Tu laimėjai!")
elif computer_choice == player_choice:
draw += 1
print("Lygiosios")
with open("rezultatai46.pkl", "wb") as file_pickle:
storage = victory_all, defeat_all, draw_all
victory_all = (victory_all + victory)
defeat_all = (defeat_all + defeat)
draw_all = (draw_all + draw)
pickle.dump(victory_all, file_pickle)
pickle.dump(defeat_all, file_pickle)
pickle.dump(draw_all, file_pickle)
elif choice == "2":
while True:
choice = input(STATISTIC)
if choice == "1":
print(f"This game session:nWon: {victory}n"
f"Lost: {defeat}nDraaw: {draw}")
elif choice == "2":
with open("rezultatai46.pkl", "rb") as file_pickle:
victory_all = pickle.load(file_pickle)
defeat_all = pickle.load(file_pickle)
draw_all = pickle.load(file_pickle)
print("Won in total:", victory_all)
print("Lost in total:", defeat_all)
print("Draw in total:", draw_all)
games_all = (victory_all + defeat_all + draw_all)
victory_percentage = ((victory_all / games_all) * 100)
print("Laimėjimai procentais: ", round(victory_percentage), "%")
elif choice == "3":
break
elif choice == "3":
print("Iki susitikimo!")
break
else:
print("Netinkamas pasirinkimas")
First – you have an error in this part:
elif computer_choice > player_choice:
computer_choice += 1
print("Tu pralaimėjai!")
It should be adding to defeat
, not computer_choice
. That is the cause of seem different behaviors for wins and losses.
Second: you are counting the current score to variables victory, defeat, draw
, since program start, and always adding the total local score to the total scores when pickling – but then you update again the total score when starting the next round.
Bear with me:
round 1:
total_victory = 0
victory = 0
Player wins:
victory = 1, total_victory += victory -> 1 and it is saved
round 2 starts at:
victory = 1
total_victory = 1
Player wins:
victory = 2
total_victory += victory -> 3
Round 3 starts at:
victory = 2
total_victory = 3
And after another win goes to:
victory = 3
total_victory += victory -> 6
The logic of this program is nice, and it can be seem you are being creative as you learn – which is great. But the fact you did not follow more usual patterns make it hard to fix without re-writting a significant part of your code.
So, as I pointed the error, I will leave it up to you to think how to fix it.
Probably the easier way is always adding "1" to total_victory, etc… as you add "1" to "victory", and never try to add "total_victory + victory" (and counterparts).