这是我目前正在处理的代码,但我的老板对它的最后一部分不太满意。有人能帮我吗?下面的问题已标记。提前致谢!
#!/usr/bin/python
def codeNameDict(lst, cnDict):
key = int(lst[0])
value = lst[1]
cnDict[key] = value
return cnDict
def realNameDict(lst, rnDict):
key = int(lst[0])
value = lst[2].strip()
rnDict[key] = value
return rnDict
def getCodeKey(keys):
import random
index = random.choice(keys)
return index
def searchKey(cnDict,value):
for x in cnDict.keys():
if(value == cnDict.get(x)):
return x
break;
def namegen(cnDict):
index1 = getCodeKey(cnDict.keys())
index2 = getCodeKey(cnDict.keys())
if(index1 == index2):
while(index1 == index2):
index2 = getCodeKey(cnDict.keys())
indexgen = [index1,index2]
else:
indexgen = [index1,index2]
return indexgen
cnDict = {}
rnDict = {}
f = open("namefile.txt")
try:
for line in f:
words = line.split(":")
cnDict = codeNameDict(words,cnDict)
rnDict = realNameDict(words,rnDict)
finally:
f.close()
# PROBLEMATIC SECTION #
for x in range(10):
indexgen = namegen(cnDict)
key1 = indexgen[0]
key2 = indexgen[1]
#print indexgen
#print key1
#print key2
pokemon1 = cnDict[key1]
pokemon2 = cnDict[key2]
realname1 = rnDict[key1]
realname2 = rnDict[key2]
print "Partners-> %s:%s" % (pokemon1, pokemon2)
print "RealNames-> %s:%s" % (realname1, realname2)
del cnDict[key1]
del cnDict[key2]