我在这里比较新,所以请告诉我是否有任何我应该知道的事情或任何错误我都是明智的做法!
我试图通过随机选择将字符添加到字典中,但我的代码似乎不起作用!
文件:sports.txt
Soccer, Joshua
Lacrosse, Naome Lee
Soccer, Kat Valentine
Basketball, Huong
Tennis, Sunny
Basketball, Freddie Lacer
Run Code Online (Sandbox Code Playgroud)
我的代码到目前为止:
def sportFileOpen():
sportFile = open("sport.txt")
readfile = sportFile.readlines()
sportFile.close()
return(readfile)
def sportCreateDict(sportFile):
sportDict = {}
for lines in sportFile:
(sport, name) = lines.split(",")
if sport in sportDict:
sportDict[sport].append(name.strip())
else:
sportDict[sport] = [name.strip()]
return(sportDict)
def sportRandomPick(name, sport, sportDict):
if sport in sportDict:
ransport = random.choice(sportDict.keys())
sportDict[ransport].append(name)
print(name, "has been sorted into", ransport)
def main():
sportFile = sportFileOpen()
sportDict = sportCreateDict(sportFile)
name = …Run Code Online (Sandbox Code Playgroud)