2016-04-22 52 views
0

我正在创建一些基本ATM的Python代码。我遇到的问题是我无法得到结果,我希望它的打印“<”函数Account.balance在0x012CBC90>“而不是实际的余额号。到目前为止,我只使用jsmith进行了测试。随时可以提出任何其他可能会导致问题的问题。如何在涉及两个类时正确使用__repr__

class Account: 

    def __init__(self,user,pin,balance): 
     self.user = user 
     self.pin = pin 
     self.balance = int(balance) 

    def get_user(self): 
     return self.user 

    def get_pin(self): 
     return self.pin 

    def balance(self): 
     return int(self.balance) 

    def setBalance(self,newBalance): 
     self.balance = newBalance 

    def __repr__(self): 
     return str(self.user) + " " + str(self.pin) + " " + str(self.balance) 


class ATM: 

    def withdraw(self,Person,amount): 
     result = Person - amount 
     return result 


    def check(self,Person): 
     Person = Account.balance 
     return str(Person) 

    def transfer(self,id1,id2): 
     pass 

    def __str__(self): 
     return self 



    def main(): 

    Chase = ATM() 

    Database = [] 

    Teron_Russell = Account("trussell",1738,0) 
    Joe_Smith = Account("jsmith",1010,1350) 

    print(Teron_Russell) 

    Database.append(Teron_Russell) 
    Database.append(Joe_Smith) 

    print("Welcome to the ATM") 
    id = input("Please enter your user ID: ") 
    pin = input("Enter your pin: ") 
    chosen = "" 

    for i in Database: 
     print("Test1") 
     name = Account.get_user(i) 
     print(name) 
     checkPin = Account.get_pin(i) 
     print(checkPin) 
     if id == name and pin == checkPin: 
      chosen = i 

    choice = input("What would you like to do. (Type 'Check','Withdraw','Transfer': ") 

    if(choice == "Check" or "check"): 
     print(Chase.check(chosen)) 



    # if(choice == "Withdraw" or "withdraw"): 
    #  wAmount = eval(input("How much would you like to Withdraw: ")) 
    # # Chase.withdraw(Account.balance,) 
    # elif(choice == "Check" or "check"): 
    #    Chase.check() 
    # else: 
    #  print("Invalid Choice!") 

if __name__ == "__main__": 
    main() 

回答

2

您将一个变量和一个方法命名为相同的名称,所以解释器会混淆使用哪一个。更改方法或变量balance的名称,您将不会遇到此问题。另外,这不是java,你不应该无缘无故地使用类。由于您没有使用任何实例变量,因此将该类中的所有方法都设置为毫无意义。

+0

谢谢,没有注意到我给他们起了同样的名字。 – WannaBeCoder

相关问题