如何在python中永久存储用户输入

时间:2016-04-06 19:35:23

标签: python python-3.x

我有一个高中项目,在python中制作一个虚假的ATM模型,如何将用户输入永久存储在文件中,然后再使用

假设我有新用户并且我想存储他的名字和别针以及他存入的钱,我该如何存储它以便下次程序运行或用户回来检查或取出钱并进入可以使用if语句检查引脚或名称是否正确,并向他显示详细信息。

我在想,当用户输入他的名字作为变量时,我们会将其存储在一个文件中,但我不知道如何准确地执行该操作,之后我们又如何再次运行if语句来检查引脚是否正确。

print("***********WELCOME TO THE BANK OF LOSS**************")
def make_an_acc():
    print("by maing an account in our bank your sure to lose all you money\n")
    print("plss enter your name\n")
    newname = input("\n")
    print("plss enter an insecure 4 digit code that can be hacked by any one \n")
    newpin = int(input("\n"))
    print("pls enter the amount you want to loss i mean deposit\n")
    newamount = int(input("\n"))

 def datastore():
   def namelist:
   namefile = open("namelist","wb");
   namefile.write(newname)
   shi = namefile.read(); `

我知道我的代码不完整,但这就是我写的

3 个答案:

答案 0 :(得分:0)

我认为您可以将其保存在列表或词典中,然后使用,pickle或cpickle(python 2x)以纯文本格式保存

这很容易使用且快速

import pickle
pickle.dump(obj, file[, protocol])

然后

pickle.load(file)

协议可以是0或1

  

如果省略protocol参数,则使用协议0。如果protocol被指定为负值或HIGHEST_PROTOCOL,则将使用最高协议版本。

参见示例 https://pymotw.com/2/pickle/

这是官方文件 https://docs.python.org/2/library/pickle.html

答案 1 :(得分:-1)

基本上,您需要某种持久性的数据。

Python有许多模块来处理它。检查:https://docs.python.org/2/library/persistence.html

答案 2 :(得分:-1)

检查代码中的注释......

print("***********WELCOME TO THE BANK OF LOSS**************")
def make_an_acc():
    print("by maing an account in our bank your sure to lose all you money\n")
    print("plss enter your name\n")
    newname = input("\n")
    print("plss enter an insecure 4 digit code that can be hacked by any one \n")
    newpin = int(input("\n"))
    print("pls enter the amount you want to loss i mean deposit\n")
    newamount = int(input("\n"))
    # return the values, so they can be used outside the function
    return [newname, newpin, newamount]

def datastore(newname):
   """ you hand this function a newname and it writes it to a file """
    namefile = open("namelist","wb");
    namefile.write(newname)
    # close file after reading or writing
    namefile.close()
    # do the reading elsewhere - to store data, you don't need it
    # so delete this for now: shi = namefile.read(); 

# you can now do this to get a list of three elements: 
# name, pin, balance:
customer_details = make_an_acc()

# customer name was the first element, so to store it in a file:
datastore(customer_details[0])

我没有时间发挥这种光彩,但是这里使用词典和json快速重写,这样可以让你创造新客户,加载现有的,获得客户详细信息和这种性质的典型事物。也许它会给你一些灵感:

import json

customers_file = "customers.json"

customers_dict = {}

def load_customers(filename=customers_file):
    """ you hand this function a filename and it returns a dictionary of all customers """
    with open(filename, "r") as customers_json:
        return json.loads(customers_json.read())

def save_customers(customers_dict, filename=customers_file):
    """ you hand this function a dictionary and a filename to save the customers """
    with open(filename, "w") as customers_json:
        customers_json.write(json.dumps(customers_dict)) 

def add_customer():
    """ pretty much your existing code - just return a dictionary instead """
    print("by maing an account in our bank your sure to lose all you money\n")
    print("plss enter your name\n")
    newname = input("\n")
    print("plss enter an insecure 4 digit code that can be hacked by any one \n")
    newpin = int(input("\n"))
    print("pls enter the amount you want to loss i mean deposit\n")
    newamount = int(input("\n"))
    # return the values, so they can be used outside the function
    customer_dict = {"name":newname, "pin":newpin, "balance":newamount}
    return customer_dict

def get_customer_details(customers):
    import pprint
    customer_name = input("Get details for which customer?")
    pprint.pprint(customers[customer_name])

# load existing
customers_dict = load_customers()
# add new
new_customer = add_customer()
# add single customer to "master" dictionary
customers_dict[new_customer["name"]] = new_customer
# save all
save_customers(customers_dict)
# get customer details by name
get_customer_details(customers_dict)