2017-12-03 32 views
0

私は銀行システムを作成するために私が配偶者であるプロジェクトに取り組んでいます。私はプログラムを完了し、それは完全に動作します私が助けが必要な唯一の問題は、サインアップのためのユーザーデータを格納する登録フォームを作成する方法と、txtファイルからのログイン用のデータを読み取ることです。どのように私はpythonで.txtファイルにuseresデータを格納する登録フォームを作るのですか?

              = 
balance = 100 

def log_in(): 
    tries = 1 
    allowed = 5 
    value = True 
    while tries < 5: 
     print('') 
     pin = input('Please Enter You 4 Digit Pin: ') 
     if pin == '1234': 
      print('') 
      print("    Your Pin have been accepted!   ") 
      print('---------------------------------------------------') 
      print('') 
      return True 
     if not len(pin) > 0: 
      tries += 1 
      print('Username cant be blank, you have,',(allowed - tries),'attempts left') 
      print('') 
      print('---------------------------------------------------') 
      print('') 
     else: 
      tries += 1 
      print('Invalid pin, you have',(allowed - tries),'attempts left') 
      print('') 
      print('---------------------------------------------------') 
      print('') 

    print("To many incorrect tries. Could not log in") 
    ThankYou() 




def menu(): 
     print ("   Welcome to the Python Bank System") 
     print (" ") 
     print ("Your Transaction Options Are:") 
     print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
     print ("1) Deposit Money") 
     print ("2) Withdraw Money") 
     print ("3) Check Balance") 
     print ("4) Quit Python Bank System.pyw") 





def option1(): 
     print ('      Deposit Money'  ) 
     print('') 
     print("Your balance is £ ",balance) 
     Deposit=float(input("Please enter the deposit amount £ ")) 
     if Deposit>0: 
      forewardbalance=(balance+Deposit) 
      print("You've successfully deposited £", Deposit, "into your account.") 
      print('Your avalible balance is £',forewardbalance) 
      print('') 
      print('---------------------------------------------------') 
      service() 

     else: 
      print("No deposit was made") 
      print('') 
      print('---------------------------------------------------') 
      service() 




def option2(): 
    print ('      Withdraw Money'  ) 
    print('') 
    print("Your balance is £ ",balance) 
    Withdraw=float(input("Enter the amount you would like to Withdraw £ ")) 
    if Withdraw>0: 
     forewardbalance=(balance-Withdraw) 
     print("You've successfully withdrawed £",Withdraw) 
     print('Your avalible balance is £',forewardbalance) 
    if Withdraw >= -100: 
     print("yOU ARE ON OVER YOUR LIMITS !") 
    else: 
     print("None withdraw made") 




def option3(): 
    print("Your balance is £ ",balance) 
    service() 




def option4(): 
    ThankYou() 


def steps(): 
    Option = int(input("Enter your option: ")) 
    print('') 
    print('---------------------------------------------------') 
    if Option==1: 
     option1() 
    if Option==2: 
     option2() 
    if Option==3: 
     option3() 
    if Option==4: 
     option4() 
    else: 
     print('Please enter your option 1,2,3 or 4') 
     steps() 

def service(): 
    answer = input('Would you like to go to the menu? ') 
    answercov = answer.lower() 
    if answercov == 'yes' or answercov == 'y': 
     menu() 
     steps() 
    else: 
     ThankYou() 

def ThankYou(): 
    print('Thank you for using Python Bank System v 1.0') 
    quit() 


log_in() 
menu() 
steps() 

私は私のプログラムは、サインアップのためのユーザー・データを格納し、.txtファイルからのログインのためのデータを読み込みます登録フォームを持っていることを期待しています。

+0

.txtはデータを格納するのに適していませんが、現在のコードでは複雑です。まず、最後の関数であるThankYou()は、カーネルを閉じて(line quit())終了したすべてを消去します。第二に、関数のどれもがどんな値も返しません。またはグローバル値を使用しないでください。第3に、データフレームでは、severalsユーザーを持つことができます。ピンがユーザーに対応するかどうかを確認し、データフレーム内の自分のアカウントにアクティビティを登録します。 – Mathieu

答えて

0

私はそれをちょっと見て、.txtファイルでやろうとしました。

1234 1000.0 
5642 500 
3256 50.0 
6543 25 
2356 47.5 
1235 495 
1234 600000 

PINとのバランスが集計で区切られています。

だから、の.pyファイルと同じフォルダに、このようなdata.txtをファイルを作成します。

次にコードでは、グローバル変数を使用して、PIN番号、残高、およびデータファイルへのパスの両方を渡します。次に、データファイルがオープンされ、天びんとピンが2つのリストに配置されます。 pandaのようなデータフレームで作業したい場合は、dictionnary構造がより適切です。

# Create the global variable balance and pin that will be used by the function 
global balance 
global pin 

# Path to the data.txt file, here in the same folder as the bank.py file. 
global data_path 
data_path = 'data.txt' 
data = open("{}".format(data_path), "r") 

# Create a list of the pin, and balance in the data file. 
pin_list = list() 
balance_list = list() 
for line in data.readlines(): 
    try: 
     val = line.strip('\t').strip('\n').split() 
     pin_list.append(val[0]) 
     balance_list.append(val[1]) 
    except: 
     pass 

# Close the data file  
data.close() 

""" Output 
pin_list = ['1234', '5642', '3256', '6543', '2356', '1235', '1234'] 
balance_list = ['1000', '500', '-20', '25', '47.5', '495', '600000'] 
""" 

次に、天びんを変更するすべての関数は、グローバル変数値を変更する必要があります。 '行をスキップするには、単に追加

  • 代わりに、印刷の(') '\ n' はへ:

    def log_in(): 
        global balance 
        global pin 
        tries = 1 
        allowed = 5 
        while tries < 5: 
         pin = input('\nPlease Enter You 4 Digit Pin: ') 
         if pin in pin_list: 
          print('\n    Your Pin have been accepted!   ') 
          print('---------------------------------------------------\n') 
          balance = float(balance_list[pin_list.index(pin)]) 
          menu() 
         else: 
          tries += 1 
          print('Wrong PIN, you have {} attempts left.\n'.format(allowed - tries)) 
          print('---------------------------------------------------\n') 
    
        print('To many incorrect tries. Could not log in.') 
        ThankYou() 
    

    または::

    def option1(): 
        global balance 
        print ('      Deposit Money\n') 
        print('Your balance is £ {}'.format(balance)) 
        deposit = float(input('Please enter the deposit amount £ ')) 
        if deposit > 0: 
         balance = balance + deposit 
         print("You've successfully deposited £ {} into your account.".format(deposit)) 
         print('Your avalible balance is £ {}\n'.format(balance)) 
         print('---------------------------------------------------') 
         service() 
    
        else: 
         print('No deposit was made. \n') 
         print('---------------------------------------------------') 
         service() 
    

    その他改善のソースを例えば印刷された文字列。

  • 値を置くための 'blabla {} ...'。format({}に入れる値)、str、またはstrの途中のものを使用します。同じ文字列に複数の値を置くことができるので、それを使うのは良いことです。保存データ部分のイラストは以下の通りです。

最後に、新しい残高は.txtファイルに保存する必要があります。これは、あなたが機能に感謝して行われます。

def ThankYou(): 
    global balance 
    global pin 
    global data_path 
    balance_list[pin_list.index(pin)] = balance 

    data = open("{}".format(data_path), "w") 
    for i in range(len(pin_list)): 
     line = '{}\t{}\n'.format(str(pin_list[i]), str(balance_list[i])) 
     data.write(line) 
    data.close() 

    print('Thank you for using Python Bank System v 1.0') 
    quit() 

私はあなたがそれを行うには、すべてのキーを持っている必要があり、あなたがアイデアを得る願っていますし、コードの修正、自分で管理することができます。

幸運を祈る!

+0

これは大いに助けになりましたが、ありがとうございます。しかし、新しいユーザーを特別なPINで登録させ、.txtファイルに書き込んで将来使用できるようにするにはどうすればよいですか? – Fouad

+0

あなたの関数のどこかにオプションの新しいPINを追加し、機能を実行する前に新しいPINと新しい残高(ユーザ入力)をpin_listとbalance_listのリストに追加するだけで、data.txtファイルを上書きします。 – Mathieu