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

2024-06-28 20:47:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为预算编制一个程序,我需要对它进行编程,以便无论何时运行代码,我都可以将我那周挣的钱加到总数中。我希望能够存储最新输入的金额pay_this_week,并在每次运行代码时将其添加到total。我是否应该将pay_this_week放入一个列表并将其附加到total中

代码如下:

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

Tags: 代码程序youformatmessage编程thispay
2条回答

当程序运行时,它们使用的任何变量都存储在ram中,每当您关闭程序时,ram就会丢失。如果您希望在运行程序的两次之间存储数据,则需要将它们保存到文件中

幸运的是,python有一些非常有用的函数来实现这一点

您的代码看起来非常整洁,可以正常工作,我们只需要添加文件读写

首先,您需要打开我们用来存储总数的文件,使用带有"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

但是,当您第一次运行该程序时,该文件将不存在(因为我们还没有创建),并且总数将为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 0

file.close() # close file for now

然后你可以运行你的代码,在我们想要存储新的总数之后,这次在写模式下打开文件"w",这将从文件中删除旧的总数

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

现在,下次运行程序时,它将加载此总数,并将正确相加

都在这里

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

你就快到了,不要放弃。2个主要错误:

  1. 如果你想让break永远持续下去,就没有必要while
  2. 在python中,idention起到了很大的作用,只有ident的行在while中,我猜您希望所有的行都在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? 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? 

相关问题 更多 >