如何永久存储用户输入以便将来添加到另一个变量?

我正在尝试制作一个预算程序,我需要对其进行编程,以便每当我运行代码时我都可以将我那周赚的钱添加到总额中。我希望能够存储最新输入的金额pay_this_week并将其添加到total每次运行代码时。我应该制作pay_this_week一个列表并将其附加到总计中吗?


这是代码:


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


芜湖不芜
浏览 189回答 2
2回答

婷婷同学_

当程序运行时,它们使用的任何变量都存储在内存中,每当您关闭程序时,内存就会丢失。如果您希望在运行程序时存储数据,则需要将它们保存到文件中。幸运的是,Python 有一些非常有用的函数可以做到这一点。您的代码看起来非常整洁并且可以正常工作,我们只需要添加文件读取和写入首先,您需要使用读取模式打开我们用来存储总计的文件"r"file = open("total.txt", "r") # open the file in read modedata = file.readline() # read the linetotal = int(data) # get the total as an int然而,当你第一次运行程序时,这个文件还不存在(因为我们还没有创建它),总数将为 0。我们可以使用一个块try来捕获这个文件,并使用模式创建一个新文件"w+",这将如果同名文件不存在则创建一个文件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 0file.close() # close file for now然后你可以运行你的代码,在我们想要存储新的总计之后,这次以写入模式打开文件"w",这将从文件中擦除旧的总计file = open("total.txt", "w") # wipe the file and let us write to itfile.write(str(total)) # write the datafile.close() # close the file现在,下次运行程序时,它将加载此总数并正确相加!这就是全部内容,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 filemoney_earnt()

有只小跳蛙

你已经快到了,不要放弃。2个主要错误:break如果while你想让它永远持续下去就没有必要在Python中,标识产生了所有的差异,只有标识的行才进入到while中,我猜你希望所有的行都在里面。这是我的尝试:    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()我的输出是:How much money did you earn this week? 4You've earnt £4 this week!You have earned £4 in total!How much money did you earn this week? 5You've earnt £5 this week!You have earned £9 in total!How much money did you earn this week? 6You've earnt £6 this week!You have earned £15 in total!How much money did you earn this week? 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python