How to keep the previous result after the next code launching

In the project ‘Games of chance’ ( I was asked to create a variable ‘total’ where I put the results of all games.

In the beginning of the code I declare this variable: total = 100.
Every time I launch the program again, this variable equals 100 again and again (this nulls the results of the previous launching the program).

How can I make program to keep previous results? This will help me to collect results as in the real casino.

1st launching the code: total = 100, result = 50, total = 150
2nd launching the code: total = 150, result = 50, total = 200

Thank you.


It sounds like you are looking for a way to save data.

I usually do this by writing my data to a json file and on launch load this data if there is any. This is not very safe tho. People could just open the json file and cheat the points.
You don’t necessarily need to use json format either. A simple .txt file would give the same results.

If you like to watch a tutorial on this you can look here to get a start.
If you rather read up on it you could start here.

NOTE: This can not be used in codecadamy. Users are not allowed to create data files on the codecadamy editor.

Hope this helps.


biirra, thank you so much. This is namely the thing I was looking for.

1 Like

@0040401666 You can code a fifth function; perhaps call it run_game().

It can have a menu:1 - "Coin Flip", 2 - "Cho Han", etc. It can have a while loop with choices like "play again? ", "choose game", "How much do you want to bet?", and"What is your call (heads/tails, odd/even, red/black)", etc. It can have a dictionary that can record your cumulative winnings at each game, along with total winnings. The possibilities are endless.

As @biirra says, it won’t hold your winnings overnignt, as a saved file would, but it can save them during a single session of playing as many games in any combination that you like.


Thank you. Would you, please, say to me the name of the function to save results during a single session (if it has been bult in Python or it’s libraries)? If the function doesn’t exist, may be, there is some information, how to creat this function by myself.

Hello – The results would be saved in a data type (int works for a running total, but list or dictionary would be better, as it could then keep track of which games have been played). The data type would be used within a function. Typically the function would use a while loop to “stay alive”. At this point, the course has not covered loops, but basically, a while loop tests if a condition is true each time around. If the condition is true, the loop repeats; if not, the loop exits.

I have written a primitive version of this for you. Unfortunately, it won’t run in the CA editor, because it uses the input() function, and the editor, wont accept input() without throwing an error. I think it is a security measure. Anyway, you’ll need to run it on your local Python installation (Thonny is extremely easy to install and use), or a different online IDE, such as

You can see that it just stores the results in a variable called money. The function menu() displays the menu options, and the function game_play() contains the while loop that keeps the game going. As long as the game runs, the variable money will continue to live.

As I said, it is very primitive. For one thing, there is no error checking at all, so any bad imput will just crash the program.

For simplicity, it only works with the first two games, Coin Flip and Cho Han. You must include those functions yourself. Run it by choosing an amount of money to start with, say 100, and typing game_play(100).

def menu(money):
  print("you have {} dollars".format(money))
  quit = input("Do you want to play? (y/n)")
  if quit == "n":
      return 0,0,0               
  print("1. Coin Flip \n2. Cho Han")
  game = input ("Which game do you want to play? Enter 1 or 2: ")
  if game == "1":
    choices = input("Heads or Tails (h/t)")    
    if choices == 'h':
      choices = "Heads"
      choices = "Tails"
  if game == "2":
    choices = input("Odd or Even? (o/e)")
    if choices == 'o':
      choices = "Odd"
      choices = "Even"
  bet = input("How much is your bet?")  
  return game, choices, bet

def game_play(money):
  game = -1
  while game != 0:
      game, choices, bet = menu(money)
      if game == 0:
          print("You have {} dollars left. Good-bye".format(money))
          bet = int(bet)
          if game == "1":
            money += coin_flip(bet, choices) 
          elif game == "2":
            money += cho_han(bet, choices)      

1 Like

patrickd314, thank you very much for the solution:
I tried your code in Jupiter and it work!
I’ve caught the idea: once to throw the variable ‘money’ in a ‘while’ loop and to keep it until the end of the game. This will save the variable from constant renewing.

I also understood: to keep the playing track I should use ‘list’ or ‘dictionary’.

Second thanks for showing me a new function: input.

1 Like