Python:如何更新多个地址行

时间:2018-01-11 15:50:33

标签: python

我目前正在使用Python为Uni开发银行系统。我是Python和编程的新手,所以我可以很容易地对很多事情感到困惑,所以如果我用错误的词来称呼,我会提前道歉。

我有5个模块:PersonAccountCustomerAdminbank_system

我的问题是尝试更新地址。我使用类似的代码更新名称,但问题是名称只有一个字。这会导致问题,因为地址存储为4个单独的部分,如__init__方法中所示。实际更新地址的方法是位于底部附近的run_profile_options()方法。

这是我输入更新地址后打印地址的方式(使用Red House,Red Drive,Redplace,F11 1FF)。

Your Profile Settings Options Are:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) Update name
2) Update address
3) Print details
4) Back

Choose your option: 3
Name Adam:
Address: 14
         Wilcot Street
         Bath
         B5 5RT

Account balance: 5000.00

使用我已经使用的python在更新地址时返回此信息。

Your Profile Settings Options Are:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) Update name
2) Update address
3) Print details
4) Back

Choose your option: 2

 Please enter a new address
: Red House, Red Drive, Red Place, F11 1FF

Your Profile Settings Options Are:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) Update name
2) Update address
3) Print details
4) Back

Choose your option: 3
Name Adam:
Address: R
         e
         d


Account balance: 5000.00

我需要做些什么才能更新各行?

以下是完整的模块:

class Person(object):
    def __init__(self, name, password, address = [None, None, None, None]):
        #PERSON DETAILS
        self.name = name
        self.password = password
        self.address = address

    def get_address(self):
        #SEARCHING PERSON ADDRESS
        return self.address

    def update_name(self, name):
        #UPDATING PERSON NAME
        self.name = name

    def update_address(self, address):
        #UPDATING PERSON ADDRESS
        self.address = address

    def get_name(self):
        #SEARCHING PERSON NAME
        return self.name

    def print_details(self):
        #PRINTING PERSON DETAILS
        print("Name %s:" %self.name)
        print("Address: %s" %self.address[0])
        print("         %s" %self.address[1])
        print("         %s" %self.address[2])
        print("         %s" %self.address[3])
        print(" ")

    def check_password(self, password):
        #PASSWORD VALIDITY CHECK
        if self.password == password:
            return True
        return False

    def profile_settings_menu(self):
        #PERSON PROFILE MENU
         print (" ")
         print ("Your Profile Settings Options Are:")
         print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
         print ("1) Update name")
         print ("2) Update address")
         print ("3) Print details")
         print ("4) Back")
         print (" ")
         option = int(input ("Choose your option: "))
         return option

    def run_profile_options(self):
        #PERSON PROFILE CHOICE
        loop = 1           
        while loop == 1:
            choice = self.profile_settings_menu()
            if choice == 1:
                #UPDATING NAME
                name=input("\n Please enter new name\n: ")
                self.update_name(name)
            elif choice == 2:
                #UPDATING ADDRESS
                address=input("\n Please enter a new address\n: ")
                self.update_address(address)
            elif choice == 3:
                #PRINTING OWN DETAILS
                self.print_details()
            elif choice == 4:
                #GO BACK
                loop = 0

1 个答案:

答案 0 :(得分:0)

您的问题可以说是update_address()方法。目前,更新的地址不会拆分为四个地址段的列表,因为address实例变量似乎期望。而是将它存储为一个普通字符串。然后,当您打印地址时,实际上是打印地址字符串的前几个字符而不是地址列表的前几个元素。

因此,您首先需要选择Person中应该负责将地址拆分为多个部分的方法。在将地址传递给run_profile_options()之前,您可以在update_address()中执行此操作,或者可以在update_address()中执行此操作。

如果选择前者,则需要通过添加以下行来更新run_profile_options()

[address_part.strip() for address_part in address.split(',')]

之后,run_profile_options()应如下所示:

def run_profile_options(self):
    # ...
    while loop == 1:
        # ...
        elif choice == 2:
            address = input("\n Please enter a new address\n: ")
            # Split the address into four parts.
            address = [address_part.strip() for address_part in address.split(',')]
            self.update_address(address)

如果您选择后者,则需要将该行代码添加到update_address()

def update_address(self, address):
    # Split the address into four parts.
    self.address = [address_part.strip() for address_part in address.split(',')]

请注意,将地址拆分为多个部分的代码假定每个部分都以逗号字符分隔。如果情况不是这样,那么您需要更新我的代码以考虑其他分隔符。