微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试制作一个预算程序,因此我需要对它进行编程,以便每当运行代码时,我都可以将当周的总收入加起来。我希望能够存储最新输入的金额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()

解决方法

运行程序时,它们使用的任何变量都存储在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中,标识产生了很大的不同,只有被标识的行才进入了一会儿,而我猜测您希望所有行都在其中。

这是我的尝试:

    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? 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?