如何解决如何永久存储用户输入以便将来添加到另一个变量?
我正在尝试制作一个预算程序,因此我需要对它进行编程,以便每当运行代码时,我都可以将当周的总收入加起来。我希望能够存储最新输入的金额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个主要错误:
- 如果您希望
break
永远持续下去,则无需while
- 在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 举报,一经查实,本站将立刻删除。