Python ATM机简化并使用更多用户(Python ATM Machine simplifying and to use further users)

编程入门 行业动态 更新时间:2024-10-28 10:26:17
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(): 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.")

I have created python code like this for my ATM machine code-transfering. Since I am not skilled at python, I created code one by one. (I have further codes that are to transfer from other users to transfer to another users.)

I have identified five users at the beginning of the code.

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)

But the problem is that I will have to add more users into the array that I've made. And based on transfer code that I've made, I can't do anything with new users that will be created.

Is there a way for me to simplifying transfering code using Customer ... so that I can add more new users into the array and do some banking works with those newly created users?

最满意答案

一般来说: 每当你看到自己重复的代码时,问问自己:“跆拳道,不可能在循环中完成吗?”

更具体:

如果您为每个客户指定他自己的号码以便轻松识别他,则会更清楚。 因此,您可以添加每次创建新客户时增加的类属性。

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

这里是行为/它是如何工作的:

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

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

In General: Every time you see code repeating itself, ask yourself: "Tae, couldn't that be done in a loop?"

More Specific:

It is clearer if you assign each customer his very own number to identify him easily. Therefore you could add a class attribute which increments every time a new customer is created.

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

you can know add each customer to a dictionary instead of a list, to easily access each customer by his index.

CustomerArchive = {} CustomerArchive[c1.index] = c1 CustomerArchive[c2.index] = c2 CustomerArchive[c3.index] = c3 CustomerArchive[c4.index] = c4

now we can easily iterate through that dictionary, to make your user interface flexible. For example...

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

...becomes...

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))

and the awful long elif part can be replaced by simply calling the specific customer:

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

EDIT

In case you want to keep your list rather than having a dictionary (for whatever reason...) nothing changes despite the part where you...

...add customers to your list (which is just as you've done it in your original question)

...try to get a specific customer from the list.

In order to get a specific CustomerObject from the list, you have to iterate through it and compare every single object with your aim index:

#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

And here is the behaviour/how it works:

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

Note: That might not be that easy to understand if you are relatively new to python, but its a good way to achieve it and quite pythonic. You could of course instead use a single function and pass the full CusomerList on every time you call the function (simpler but not as elegant...).

更多推荐

本文发布于:2023-08-04 23:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更多   用户   ATM   Python   simplifying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!