0

I am trying to make a program for budgeting, I need to program it so that whenever I run the code I can add how much money I earned for that week to the total. I want to be able to store the newest inputted amount pay_this_week and add it to the total each time I run the code. Should I make pay_this_week into a list and append it to total?

Here is the code:

def money_earnt():
    total = int()
    while True:
        try:
            pay_this_week = int(input("How much money did you earn this week? "))
            break
        except ValueError:
            print("Oops! That was no valid number. Try again...")

    pay_this_week_message = "You've earnt £{0} this week!".format(pay_this_week)
    total = pay_this_week + total
    total_message = "You have earned £{0} in total!".format(total)
    print(pay_this_week_message)
    print(total_message)

money_earnt()
martineau
  • 119,623
  • 25
  • 170
  • 301
winterwolf
  • 17
  • 2

2 Answers2

0

When programs are run then any variables they use are stored in ram, which is lost whenever you close the program. If you want your data to be stored between times when you run your program you're going to need to save them to a file.

Luckily python has some really helpful functions to do this.

Your code as is looks very neat and will work just fine, we just need to add the file reading and writing

first you'll want to open the file that we use to store the total, using read mode with "r"

file = open("total.txt", "r") # open the file in read mode
data = file.readline() # read the line
total = int(data) # get the total as an int

however when you first run the program this file won't exist yet (as we haven't made it) and the total will be 0. We can use a try block to catch this, and create a new file using the "w+" mode, which will create a file if one with that name does not exist

total = int()

try: # try open the file
    file = open("total.txt", "r") 
    data = file.readline()
    total = int(data)
except: # if file does not exist
    file = open("total.txt", "w+") # create file
    total = 0 # this is the first time we run this so total is 0

file.close() # close file for now

then you can run your code, and after we want to store the new total, opening the file this time in write mode "w", this will wipe the old total from the file

file = open("total.txt", "w") # wipe the file and let us write to it

file.write(str(total)) # write the data

file.close() # close the file

now the next time you run your program it will load this total and will be added up properly!

Here is it all together,

def money_earnt():

    total = int()

    try: # try open the file
        file = open("total.txt", "r") 
        data = file.readline()
        total = int(data)
    except: # if file does not exist
        file = open("total.txt", "w+") # create file
        total = 0
    
    file.close() # close file for now


    while True:
        try:
            pay_this_week = int(input("How much money did you earn this week? "))
            break
        except ValueError:
            print("Oops! That was no valid number. Try again...")

    pay_this_week_message = "You've earnt £{0} this week!".format(pay_this_week)
    total = pay_this_week + total
    total_message = "You have earned £{0} in total!".format(total)
    print(pay_this_week_message)
    print(total_message)


    file = open("total.txt", "w") # wipe the file and let us write to it
    file.write(str(total)) # write the data

    file.close() # close the file

money_earnt()
Jay
  • 2,553
  • 3
  • 17
  • 37
  • 1
    Bloody life-saver Jay, i had just been looking around for how to do this when you came in with a perfect explanation of how to do this. Out of curiosity, could i use sys.exit if the input == "stop", or something to this extent? Thank you either way! – winterwolf Sep 19 '20 at 16:44
  • I would recommend to use JSON to save python variables as a string in file. – Yossi Levi Sep 19 '20 at 17:15
  • @winterwolf you could absolutely do that, it also might be worth checking for the input being _"clear"_ or something similar to reset the total to 0 – Jay Sep 19 '20 at 17:21
  • Whenever i try to do so it doesnt do anything, im doing: if pay_this_week == "stop": sys.exit() – winterwolf Sep 19 '20 at 17:31
  • I think I would need to see the surrounding code to help you any more. Why not create a new question and ask this? :) you can link to it here and i'll take a look. – Jay Sep 19 '20 at 17:33
  • i will do, thank you Jay (sorry my replies are slow im building a fence at the same time...) – winterwolf Sep 19 '20 at 18:14
  • https://stackoverflow.com/questions/63971982/how-to-use-sys-exit-if-input-is-equal-to-specific-words – winterwolf Sep 19 '20 at 18:19
-1

You were almost there, don't give up. 2 main mistakes:

  1. there is no need to break the while if you want it to last forever
  2. in python, the identations made all the difference, only the lines that were ident got inside the while, and i'm guessing that you wanted all the lines to be inside.

this is my try:

    def money_earnt():

    total = int()

    while True:
        try:
            pay_this_week = int(input("How much money did you earn this week? "))
        except ValueError:
            print("Oops! That was no valid number. Try again...") 
            continue

        pay_this_week_message = "You've earnt £{0} this week!".format(pay_this_week)
    
        total = pay_this_week + total
    
        total_message = "You have earned £{0} in total!".format(total)
    
        print(pay_this_week_message)
        print(total_message)
   
money_earnt()

my output was:

How much money did you earn this week? 4
You've earnt £4 this week!
You have earned £4 in total!
How much money did you earn this week? 5
You've earnt £5 this week!
You have earned £9 in total!
How much money did you earn this week? 6

You've earnt £6 this week!
You have earned £15 in total!
How much money did you earn this week? 
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7