Python ATM 机简化并使用更多用户

Python ATM Machine simplifying and to use further users

def transfer():
    print ("You've chosen to transfer money.")
    transAccFrom = int(input("From which account you want to transfer?\n1.Jack 2.Smith 3.Suzy 4.Craig 5.Vic :"))
    transMoney = float(input("How much you want to tranfer?"))
    transAccTo = int(input("To which account you want to transfer?\n1.Jack 2.Smith 3.Suzy 4.Craig 5.Vic :"))
#These are asked to know from which acc to to which acc the user wants to transefer.

    if (transAccFrom == 1):
        if (transMoney > c1.balance):
            print ("The process can't be done. Please check your account balance.")
        #Since balance of the account that is sending money from isn't enough, non process is being done.
        else:
            c1.balance = c1.balance - transMoney
            if (transAccTo == 1):
                c1.balance = c1.balance + transMoney
                print ("You have transfered money from Jack's Acc to Jack's Acc. Balance of Jack's Acc is "+str(c1.balance)+". Thank you for using our bank.")
            #After eliminate the amount of money that is determined to be transfered from, the amount of money is directly added to account that the money is supposed to be transfered to. Below steps are the same stages.
            elif (transAccto == 2):
                c2.balance = c2.balance + transMoney
                print ("You have transfered money from Jack's Acc to Smith's Acc. Remaining value of Jack's Acc is "+str(c1.balance)+". Balance of Smith's Acc is "+str(c2.balance)+" Thank you for using our bank.")

            elif (transAccTo == 3):
                c3.balance = c3.balance + transMoney
                print ("You have transfered money from Jack's Acc to Suzy's Acc. Remaining value of Jack's Acc is "+str(c1.balance)+". Balance of Suzy's Acc is "+str(c3.balance)+" Thank you for using our bank.")

            elif (transAccTo == 4):
                c4.balance = c4.balance + transMoney
                print ("You have transfered money from Jack's Acc to Craig's Acc. Remaining value of Jack's Acc is "+str(c1.balance)+". Balance of Craig's Acc is "+str(c4.balance)+" Thank you for using our bank.")

        elif (transAccTo == 5):
            c5.balance = c5.balance + transMoney
            print ("You have transfered money from Jack's Acc to Vic's Acc. Remaining value of Jack's Acc is "+str(c1.balance)+". Balance of Vic's Acc is "+str(c5.balance)+" Thank you for using our bank.")

            else:
                print ("You have input the wrong account to transfer money. Please check it again.")

我已经为我的 ATM 机器代码传输创建了 python 这样的代码。由于我不擅长python,所以我一个一个写代码。 (我有更多的代码可以从其他用户转移到另一个用户。)

我在代码的开头确定了五个用户。

class Customer:
    def __init__ (self, name, birth, address, hkid, balance):
        self.name = name
        self.birth = birth
        self.address = address
        self.hkid = hkid
    self.balance = balance

#Assigning elements that are going to be in an array.
c1 = Customer ("Jack", "Jan, 10th, 1996", "430 Davis Ct., San Francisco", 
 "M8875895", 40000)
c2 = Customer ("Smith", "March 24th, 1997", "3-5 Tai Koo Shing, Hong Kong", "M3133242", 600)
c3 = Customer ("Suzy", "May 5th, 1995", "32 Clearwater Bay Ave. Hong Kong", "M8378644", 100000)
c4 = Customer ("Craig", "May 24th, 1993", "700 Powell Street, San Francisco", "M2314565", 70000)
c5 = Customer ("Vic", "September 21st, 1992", "1210 Freud Street, New York", "M1234569", 3400)

 #Appending customer information into the array
 CustomerList = []
CustomerList.append (c1)
CustomerList.append (c2)
CustomerList.append (c3)
CustomerList.append (c4)
CustomerList.append (c5)

但问题是我必须将更多用户添加到我创建的数组中。根据我制作的转移代码,我无法对将要创建的新用户执行任何操作。

有没有一种方法可以简化使用 Customer ... 传输代码,以便我可以将更多新用户添加到数组中,并与这些新创建的用户进行一些银行业务?

这可行:

def transfer(customers):
    print ("You've chosen to transfer money.")
    for i, customer in enumerate(customers):
        print("{}. {}".format(i, customer.name))
    i = int(input("Select the number of the account from which "
                  "you'd like to transfer money: "))
    fro = customers[i]
    amount = float(input("How much would you like to transfer: "))
    if fro.balance < amount:
        print ("The process can't be done. Please check your account balance.")
        return

    for i, customer in enumerate(customers):
        print("{}. {}".format(i, customer.name))
    i = int(input("Select the number of the account to which "
                  "you'd like to transfer money: "))        
    to = customers[i]

    fro.balance -= amount
    to.balance += amount
    print ("You have transfered money from {}'s account to {}'s account. "
           "The new balance of {}'s account is {}. ".format(
               fro.name, to.name, fro.name, fro.balance))

其中输入参数 customers 是您的 CustomerList

我使用 fro 而不是 from,因为 from 是 Python 中的保留字。


正如我在对问题本身的评论中提到的,我认为 dict 会比列表更好,但是由于您目前正在使用列表,所以我也使用了列表,以便留下来最接近您目前拥有的。

总的来说:
每次看到代码重复时,问问自己:"Tae, couldn't that be done in a loop?"

更具体:

  • 如果您为每个客户分配一个自己的号码以便轻松识别他,那就更清楚了。因此,您可以添加一个 class 属性,该属性会在每次创建新客户时递增。

    class Customer:
        counter = 1
        def __init__ (self, name, birth, address, hkid, balance):
            self.name = name
            self.birth = birth
            self.address = address
            self.hkid = hkid
            self.balance = balance
    
            #assign customer index, starting at 1
            self.index = Customer.counter
            Customer.counter += 1
    
  • 您可以知道将每个客户添加到字典而不是列表中,以便通过索引轻松访问每个客户。

    CustomerArchive = {}
    
    CustomerArchive[c1.index] = c1
    CustomerArchive[c2.index] = c2
    CustomerArchive[c3.index] = c3
    CustomerArchive[c4.index] = c4
    
  • 现在我们可以轻松地遍历该词典,使您的用户界面更加灵活。例如...

    transAccFrom = int(input("From which account you want to transfer?\n1.Jack 2.Smith 3.Suzy 4.Craig 5.Vic :"))
    

    ...变成...

    question = "From which account do you want to transfer?\n"
    
    for index,customer in CustomerArchive.items():
    
        question = question + str(index) + ". " + str(customer.name)
    
    transAccFrom = int(input(question))
    
  • 和可怕的长 elif 部分可以通过简单地调用特定客户来替换:

    sender = CustomerArchive[transAccFrom]
    receiver = CustomerArchive[transAccTo]
    
    #...ongoing transaction stuff
    

编辑

如果您想保留列表而不是字典(无论出于何种原因...),尽管您...

...将客户添加到您的列表 (就像你在原来的问题中所做的那样)

...尝试从列表中获取特定客户。

为了从列表中获取特定的 CustomerObject,您必须遍历它并将每个对象与您的目标索引进行比较:

#The outer function defines the default customer_list source
def get_customer_from(cus_list):
    #The inner function finds&returns the customer matching the given index
    def wrapped(customer_index):
        for customer in cus_list:
            if customer.index == customer_index:
                return customer
    return wrapped

这是有效的 behaviour/how:

>>get_customer = get_customer_from(CustomerList)
>>get_customer(1).name
"Jack"
>>Suzy = get_customer(3)
>>Suzy.birth
"May 5th, 1995"

注意:如果你对python比较陌生,那可能不太容易理解,但这是实现它的好方法,而且相当python集成电路。您当然可以改为使用单个函数并在每次调用该函数时传递完整的 CusomerList(更简单但不那么优雅...)。