2016-04-06 18 views
-2

merhaba her biri benim ilk sorum bu yüzden pls benimle çıplakım python'da ATM'nin sahte bir modelini yapmak için bir lise projem var nasıl bir dosyada kalıcı olarak kullanıcı girdisini saklayabilirim ve daha sonraPython'da bir kullanıcı girişini kalıcı olarak saklamak

numaralı telefonu kullanıyorum ve varsa onun adını ve pimini ve depoladığı parayı depolamak istiyorum. Programı bir sonraki çalıştırmada veya kullanıcı kontrol etmek veya çıkarmak için geri dönecek şekilde nasıl saklayacağım para ve pim ve onun adı doğru bir pim ve adı doğru olduğunu kontrol edip onu kontrol edebilir ve ona ayrıntıları göstermek için

Kullanıcı adı bir kullanıcı adı olarak girdiğinizde onu bir dosya ama nasıl bilmiyorum Tam olarak bunu ve üzerinde deyimi yine benim kod tam değil biliyorum

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

pim doğruluğuna emin olmadığını eğer bundan sonra nasıl sonra çalışabilir ama bu i

sayesinde yazdıklarını avans

+2

ihtiyaçlarınıza bağlı olarak, pickle', 'json',' csv' veya 'sqlite'' üzerinde okumak ve oradan gidebilirsiniz. – tzaman

cevap

-1

Kontrol ... yapmak resmi doc olduğunu Bu parlaklık ama burada yeni müşteriler kazanmak, var olan, c ustomer detayları ve bu doğadaki tipik şeyler. Belki sana biraz ilham vereceğiz:

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) 
+0

İsim pimi ve newamount –

+0

@MatrixJammr için ayrı bir işlev yapabiliriz Evet, bu kesinlikle mümkündür, ancak bunu farklı bir şekilde değiştirmek daha iyi olacaktır, bu nedenle müşteri detayına göre yeni bir işlev yazmak zorunda kalmazsınız. Yakında cevabı düzenleyeceğim. – jDo

+0

@MatrixJammr Haberinizi başka bir alma işlemi için güncelleştirilmiş yanıta göz atın – jDo

0

Sana bir liste veya dict bu kurtarabilecek düşünüyorum ve daha sonra, turşu veya cpickle (piton 2x) düz metin

bu kullanımı çok kolaydır tasarrufu için ve hızlı

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

ve

pickle.load(file) 

protokolü 0 olabilir veya 1

Protokol parametresi atlanırsa, protokol 0 kullanılır. Protokol bir negatif değer veya HIGHEST_PROTOCOL olarak belirtilirse, en yüksek protokol sürümü kullanılır.

: örnekler https://pymotw.com/2/pickle/

görmek ve bu Zamanım yoktu

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

kodunda yapılan yorumlar https://docs.python.org/2/library/pickle.html

İlgili konular