2017-11-16 3 views
-2

나는 완전히 잃어버린. 우리의 숙제가 있습니다 :ATM 계정 숙제 (클래스, 텍스트 파일, 개체)

  1. 가 자신의 이름과 PIN을
  2. 프로그램을 사용자를 묻는 ATM 프로그램을 만듭니다가, 예금, 또는 종료, 잔액을 확인 철회 할 경우 사용자 요청을 통해 루프. 사용자가 종료를 선택하면 루프가 종료됩니다. 초기 잔액은 10,000 달러입니다.
  3. 모든 거래를 기록하는 텍스트 파일을 만듭니다. 파일의 첫 번째 두 라인은 사용자의 이름과 핀
  4. 해야
  5. 프로그램의 요구를 (, 잔액을 확인, 예금 인출) 사용자로부터 정보를 표시합니다 초기화STR에게 기능을 포함라는 클래스를 생성 파일에서 읽고 객체를 만들 수 있습니다. 오브젝트의 필드는 이름, 핀 및 저울입니다. 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

입니다