2017-11-16 5 views
-2

私は完全に失われています。彼らは、残高を確認撤退、預金、または終了する場合、ユーザーに尋ねるをループするプログラムATMアカウントの宿題(クラス、テキストファイル、オブジェクト)

    1. 自分の名前をユーザーに要求するATMのプログラムを作成し、ピン:私たちの宿題をしています。ユーザーが終了を選択すると、ループが終了します。初期残高は10,000ドルです
    2. すべてのトランザクションを記録するテキストファイルを作成します。まず、ファイルの2行は、ユーザーの名前でなければなりませんし、ピン
    3. プログラムのニーズを(撤退、預金、残高を確認してください)、ユーザーからの情報を表示するには、のinitSTR機能を備えているというクラスを作成します。ファイルから読み込んでオブジェクトを作成します。オブジェクトのフィールドは、名前、ピン、および天びんです。 6.ピン番号と名前がBAccオブジェクトの名前とピン番号と一致しない場合、プログラムは終了します。それ以外の場合は、ファイルを更新する回数だけトランザクションを実行することができます。 7. try:blocksを使用してエラーを処理する必要があります。これには、無効なデータ入力と、引き出し時の資金不足が含まれます。

    これは私が今持っているコードですが、それほど多くはありませんが、接続する方法が必要です。

    accountfile=open("ATMACC.txt", "a") 
    name=input("What is your name? ") 
    pin=input("What is your Pin #? ") 
    accountfile.write("Name: " + name + "\n" + "PIN: " + pin + "\n") 
    USER_BALANCE = 10000 
    class BankAcc(object): 
        def __init__(self,name,pin,balance): 
         name = self.name 
         pin = self.pin 
         10000 = self.balance 
        def __str__(self): 
         return "Bank Account with Balance {}".format(self.name,self.balance) 
        def checkbalance(self): 
         print(self) 
        def deposit(self,amount): 
         self.amount += amount 
        def withdraw(self,amount): 
         if self.balance > amount: 
          self.balance -= amount 
         else: 
          raise ValueError 
    while True: 
        answer=input("Would you like to deposit, withdraw, check balance, or exit? ") 
        if answer=="deposit" or answer== "Deposit": 
         x= input("How much would you like to deposit? ") 
         USER_BALANCE += float(x) 
         print ("Your new balance is: $" + str(USER_BALANCE)) 
         accountfile.write("\n" + "Deposit of $" + x + "." + " " + "Current balance is $" + str(USER_BALANCE)) 
         continue 
        elif answer== "withdraw" or answer== "Withdraw": 
         y= input("How much would you like to withdraw? ") 
         if float (y)<=USER_BALANCE: 
          USER_BALANCE -= float(y) 
          print ("Your new balance is: $" + str(USER_BALANCE)) 
          accountfile.write("\n" + "Withdraw of $" + y + "." + " " + "Current balance is $" + str(USER_BALANCE)) 
          continue 
         else: 
          print ("Cannot be done. You have insufficient funds.") 
        elif answer== "check balance" or answer== "Check Balance": 
         print ("$" + str(USER_BALANCE)) 
        elif answer== "exit" or answer== "Exit": 
         print ("Goodbye!") 
         accountfile.close() 
         break 
        else: 
         print ("I'm sorry, that is not an option") 
    

    助けてください。私はこれが完全な混乱であることを知っていますが、どんな助けも大いに評価されるでしょう。

  • 答えて

    0

    あなたの間違いを説明するコメントが追加されました。質問されたことをしました。

    class BankAcc(object): 
        def __init__(self, name, pin, balance=10000.0): #If you want to give default balance if not provided explicitly do this 
         self.name = name     #variable where value is stored = expression 
         self.pin = pin 
         self.balance = balance 
        def CheckUser(self, name, pin):  #A function to check wheather the user is valid or not 
         try: 
          if name != self.name or pin != self.pin: 
           raise ValueError 
          print("Name and Pin Matches") 
         except ValueError: 
          print("Name or Password is not correct") 
          exit(0) 
    
        def __str__(self): 
         return "{}'s Bank Account with Balance {}".format(self.name, self.balance) #using only one substition in the string but providing 2 variables 
    
        def checkbalance(self): 
         return '$' + str(self.balance) #self is the instance of the class for printing balance use self.balance 
    
        def deposit(self, amount): 
         self.balance += amount  #self.ammount will create a new property but i think you want to increase your balance 
    
        def withdraw(self, amount): 
         try:       #use try catct if raising an excpetion 
          if self.balance >= amount: #balance should be greater than and equal to the ammount 
           self.balance -= amount 
          else: 
           raise ValueError 
         except ValueError:    #Handle your exception 
           print("Cannot be done. You have insufficient funds.") 
    
        def filedata(self):    #An extra function for writing in the file. 
         return "{},{},{}".format(self.name,self.pin,self.balance) 
    
    accountfile = open("ATMACC.txt", "r+") #"r+" should be used becuase you are reading and writing 
    accountfiledata=accountfile.read().split(",") #Can be done in the class it self 
    BankUser = BankAcc(accountfiledata[0],accountfiledata[1],float(accountfiledata[2])) #For simplicity i used comma seperated data and created an instance of it 
    name=input("What is your name? ") 
    pin=input("What is your Pin #? ") 
    BankUser.CheckUser(name, pin) 
    #accountfile.write("Name: " + name + "\n" + "PIN: " + pin + "\n") #You did not specify that you want to write in file as well 
    #USER_BALANCE = 10000   it is not needed 
    
    while True: #For multiple options use numbers instead of words such as 1 for deposit etc 
        answer=input("Would you like to deposit, withdraw, check balance, or exit? ") 
        if answer=="deposit" or answer== "Deposit": 
         x= input("How much would you like to deposit? ") 
         BankUser.deposit(float(x)) #use your deposit function 
         print ("Your new balance is: " + BankUser.checkbalance()) 
         #continue #Why using continue, no need for it 
        elif answer== "withdraw" or answer== "Withdraw": 
         y= input("How much would you like to withdraw? ") 
         BankUser.withdraw(float(y))   #Use Your withdraw function why reapeating your self 
        elif answer== "check balance" or answer== "Check Balance": 
         print(BankUser.checkbalance()) 
        elif answer== "exit" or answer== "Exit": 
         print ("Goodbye!") 
         accountfile.close() 
         exit(0) 
        else: 
         print ("I'm sorry, that is not an option") 
        accountfile.seek(0)    #I dont know whether you want to record all tranction or a single one 
        accountfile.truncate()   #perform write function at the last 
        accountfile.write(BankUser.filedata()) 
    

    とテキストファイル形式は

    Himanshu,1111,65612.0 
    
    ATMACC.txt

    です