格式化我的代码的正确方法是什么?

时间:2019-04-06 19:50:57

标签: python-3.x oop

  1. 我一直试图解析我的脚本,但没有结果。我不能 要显示的负载数据。它保存在members.txt中,可以读取 如果打开则手动操作。如果有人可以在 脚本作为一个整体,将不胜感激。

    我已经调停了剧本,但是仍然没有运气。

    如果在vars()中使用“ 文件”,则导入操作系统:        wk_dir = os.path.dirname(os.path.realpath(' file '))否则:        打印(“我们正在交互式运行脚本”)        class PlayerCLass(object):        名称=“”        电话=“”        数字= 0

       def __init__(self, name, phone, number):
           self.name = name
           self.phone = phone
           self.number = number
    
    
       def setname(self, name):
           self.name = name
    
    
        def setphone(self, phone):
            self.phone = phone
    
        def setnumber(self, number):
            self.number = number
    
        def getname(self):
            return self.name
    
        def getphone(self):
            return self.phone
    
        def getnumber(self):
            return self.number
    
        def display_data(self):
            print("")
            print("Member information: ")
            print("Name:", self.name)
            print("Phone number:", self.phone)
            print("Jersey number:", self.number)
    
    
    def displayMenu():
        print("===========Menu Selections===========")
        print("1. Display Team Roster.")
        print("2. Add Member.")
        print("3. Remove Member.")
        print("4. Edit Member.")
        print("5. Save Data.")
        print("6. Load Data.")
        print("9. Exit Program.")
        print("")
        return int(input("<<Selection>> "))
    
    
    def printPlayer(players):
        print("Current members: ")
        if len(players) == 0:
            print("No current members in memory.")
        else:
            x = 1
            while x < len(players):
                print(x)
                x = x + 1
    
    
    def addPlayer(players):
        newName = input("Type in member's name to be added to the roster:")
        newPhone = input("Type in the member's phone number:")
        newNumber = int(input("Type in the member's jersey number:"))
        players[newName] = playerCLass(newName, newPhone, newNumber)
        return players
    
    
    def removePlayer(players):
        name = input("Enter member's name to be removed:")
        if name in players:
            del players[name]
        else:
            print("member's name not found in list: ")
        return players
    
    
    def editPlayer(players):
        oldName = input("Enter the name you want to edit: ")
        if oldName in players:
            newName = input("Enter a new name: ")
            newPhone = input("Enter new phone number: ")
            newNumber = int(input("Enter new jersey number: "))
            players[oldName] = playerCLass(newName, newPhone, newNumber)
        else:
            print("No such name in memory: ")
        return players
    
    
    def saveData():
        filename = input("Filename to save: ")
        print("Saving data...")
        outFile = open(filename, "wt")
        for x in players.keys():
            name = players[int(x)].getname()
            phone = players[int(x)].getphone()
            number = str(players[x].getnumber())
            outFile.write(name + "," + phone + "," + number +  "\n")
        print("Data saved.")
        outFile.close()
    
    
    def loadData():
        players = {}
        filename = input("Filename to load: ")
        inFile = open(filename, "rt")
        print("Loading data...")
        while True:
            inLine = inFile.readline()
            if not inLine:
                break
            inLine = inLine[:-1]
            name, phone, number = inLine.split(",")
            players[name] = (name, phone, number)
        print("Data Loaded Successfully.")
        inFile.close()
        return players
    
    
    print("Welcome to the Team Manager")
    players = {}
    menuSelection = displayMenu()
    print()
    while menuSelection != 9:
        if menuSelection == 1:
            printPlayer(players)
        elif menuSelection == 2:
            players = addPlayer(players)
        elif menuSelection == 3:
            players = removePlayer(players)
        elif menuSelection == 4:
            players = editPlayer(players)
        elif menuSelection == 5:
            saveData()
        elif menuSelection == 6:
            loadData()
        menuSelection = displayMenu()
    
    print("Exiting Program...")
    

1 个答案:

答案 0 :(得分:0)

如果您的问题专门导致printPlayer无法正常工作,则可以

def printPlayer(players):
    print("Current members: ")
    if len(players) == 0:
        print("No current members in memory.")
    #Iterate through dictionary of player name and player object and call
    #display data on each of them
    else:
        for name, player in players.items():
            print(player.display_data())

如果您随后运行代码并根据编写的语句输入内容,则应该得到类似的内容。

Welcome to the Team Manager
===========Menu Selections===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.

<<Selection>> 1
Current members: 
No current members in memory.
===========Menu Selections===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.

<<Selection>> 2
Type in member's name to be added to the roster:Joe
Type in the member's phone number:123
Type in the member's jersey number:456
===========Menu Selections===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.

<<Selection>> 1
Current members: 

Member information: 
Name: Joe
Phone number: 123
Jersey number: 456
None
===========Menu Selections===========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.

<<Selection>>