2016-04-06 18 views
-2

こんにちは、これは私の最初の質問ですので、私と一緒に裸です。私は高等学校のプロジェクトでATMの偽のモデルを作る方法を教えています。後でそれを使用するPythonでユーザー入力を永続的に保存する方法

私は新しいユーザーがいて、私は彼の名前とピンとお金を保管したいと思っています。次回にプログラムが実行されるか、ユーザーがチェックしたり取り出したりするようにお金とピンまたは彼の名前を入力するそれはピンと名前が正しいことを確認することができますとピンと名前が正しいと彼に詳細を示す

私は変数として彼の名前を入力すると、ファイルが、私は方法を知っていないまさにそれを行うと、その上の文は、再び私は私のコードは完全ではありません知っている

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

をピンが正しいかチェックするかしない場合は、その後どのように我々は、実行することができますが、これは私がで

感謝を書いたものですadvance

+2

ニーズに応じて、あなたはpickle'、 'json'、' csv'、または '' sqlite'をよく読んで、そこから行くことができます。 – tzaman

答えて

-1

は、コード内のコメントをチェックしてくださいこれは輝いていますが、ここでは、新しい顧客を作り、既存の読み込みを可能にし、cを得ることを可能にする辞書と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) 
+0

私たちは名前ピンとnewamountのために別の機能を作ることができます –

+0

@MatrixJammrはい、確かに可能ですが、私はそれを別の方法で変更する方が良いと思いますので、顧客の詳細ごとに新しい機能を書く必要はありません。私はすぐに答えを編集します。 – jDo

+0

@MatrixJammrスクリプトの別のテイクの更新された答えを確認してください。 – jDo

0

を、私はあなたがリストや辞書でこれを救うことができると思いますし、その後、漬物やcPickleの(パイソン×2)、プレーンテキスト

で、これは非常に使いやすいです保存用と高速

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

、次いで

pickle.load(file) 

プロトコルは0であるか、または1

protocolパラメータを省略すると、プロトコル0が使用されます。プロトコルが負の値またはHIGHEST_PROTOCOLとして指定されている場合は、最高のプロトコルバージョンが使用されます。

私が作る時間がなかった

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

...例 https://pymotw.com/2/pickle/

と、これは公式のドキュメントである https://docs.python.org/2/library/pickle.html

関連する問題