如何保存用户的输入并在重新启动程序时询问他们是否要再次使用它,这样他们就不必每次关闭程序时都输入相同的内容?
您应该将数据存储在某些DB或JSON文件中,关闭程序后无法保存存储在变量中的输入数据。
你可以使用这样的东西。它将用户输入的数据保存到
config.py
模块,因此您可以在任何地方使用它。
import os
user_input = None
if os.path.exists('config.py'): #check if config file exist
ask = input("Do you want use previous data? (yes/no)")
if ask == 'no':
user_input = input("Some things...")
elif ask == 'yes':
import config
user_input = config.last_input # take last value of input from config file
else:
print("Wrong command, please anserw yes or no.")
else:
user_input = input("Some things...")
print(user_input)
# save input
with open("config.py", "w+") as file:
file.write(f"last_input = {user_input}")
很简单,不需要使用 json 或 ini 文件。您只需复制并粘贴即可。
这是我在这里发表的第一篇文章。所以。我有一个小脚本,可以根据用户输入生成形状。 GarmentBlockDrawer 类: def init(自身): 自我测量= { “脚踝”:20, '腰部到脚踝':100, “膝盖”:35, “腰部到膝盖”:50, “臀部”:30, ‘腰围’:40 事情是。我希望程序记住上次使用的测量值。 实现这一目标的最佳方法是什么?