我有我在教程后构建的代码,并且我已成功存储到文件中。我在物理上检查了文件以确保信息存在并且确实存在。但是检索它给我带来了困难。我已经盯着它看了几个小时,发现它没有任何问题。如果您按照步骤2 - 2 1 - 5 - 9并重新运行它然后执行步骤6 - 1您将注意到第一组步骤没有问题,第二组菜单选项没有。
这就是我所拥有的。
team.py类文件
#Team class definition
class Team:
#initializing object
def __init__(self, name, phone, jersey):
self.__name = name
self.__phone = phone
self.__jersey = jersey
#mutator methods
def setname(self, name):
self.__name = name
def setphone(self, phone):
self.__phone = phone
def setjersey(self, jersey):
self.__jersey = jersey
#accessor methods
def getname(self):
return self.__name
def getphone(self):
return self.__phone
def getjersey(self):
return self.__jersey
#display template for output
def displayData(self):
print("")
print("Team information: ")
print("------------------------")
print("Member's Name:", self.__name)
print("Phone Number:", self.__phone)
print("Jersey Number:", self.__jersey)
members.py程序文件
# program functions and import data
import team
def displayMenu():
print("===========Main Menu===========")
print("1. 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 printMember(members):
if len(members) == 0:
print("No members in roster yet.")
else:
for x in members.keys():
members[x].displayData()
def addMember(members):
newName = input("Enter new member's name: ")
newPhone = int(input("Enter member's phone number: "))
newJersey = input("Enter member's jersey number: ")
members[newName] = team.Team(newName, newPhone, newJersey)
return members
def removeMember(members):
removeName = input("Enter the member's name you wish to remove: ")
if removeName in members:
del members[removeName]
print("\nYou've removed ", removeName)
else:
print("This name is not in our records.")
return members
def editMember(members):
oldName = input("Enter the name of the member you want to edit: ")
if oldName in members:
newName = input("Enter the member's new name: ")
newPhone = int(input("Enter the member's new phone number: "))
newJersey = input("Enter the member's new jersey number: ")
members[oldName] = team.Team(newName, newPhone, newJersey)
else:
print("Member does not exist")
return members
def saveData(members):
filename = input("Filename to save: ")
print("Saving data...")
outFile = open(filename, "wt")
for x in members.keys():
name = members[x].getname()
phone = str(members[x].getphone())
jersey = members[x].getjersey()
outFile.write(name + "," + phone + "," + jersey + "\n")
print("Data saved.")
outFile.close()
def loadData():
members = {}
filename = input("Filename to load: ")
inFile = open(filename, "rt")
print("Loading data...")
while True:
# read in a line of text from the text file
inLine = inFile.readline()
# if line is empty, stop loading data.
if not inLine:
break
inLine = inLine[:-1]
name, phone, jersey = inLine.split(",")
members[name] = team.Team(name, phone, jersey)
print("Data Loaded Successfully.")
inFile.close()
return members
# Running the program
print("Welcome to the Team Manager")
members = {}
menuSelection = displayMenu()
while menuSelection != 9:
if menuSelection == 1:
printMember(members)
elif menuSelection == 2:
members = addMember(members)
elif menuSelection == 3:
members = removeMember(members)
elif menuSelection == 4:
members = editMember(members)
elif menuSelection == 5:
filename = saveData(members)
elif menuSelection == 6:
filename = loadData()
menuSelection = displayMenu()
print("Exiting Program...")
帮助我或我将成为一个酗酒者:)哈哈这是IDEONE
答案 0 :(得分:1)
saveData不返回任何内容,不返回文件名
filename = saveData(members)
loadData不返回文件名,它返回成员。
filename = loadData()
我建议研究全局变量如何在Python中运行。您只需要一个members
个实例。
如果您正在寻找更好的方法来执行CRUD操作,请使用Python的sqlite模块
顺便说一句,Pycharm有一个有用的调试器。