所以,我一直在研究这段代码,以确定第114届国会中共和党、民主党和无党派参议员的人数。这是我的密码。由于某些原因,我不断得到关于我的变量,以num开头的奇怪的错误。请帮助。你知道吗
def main():
# Determines the number of senators of each party affiliation
infile = open("Senate114.txt", 'r')
set1 = {line.rstrip() + "\n" for line in infile}
infile.close()
listx = list(set1)
listx.sort(key=lambda x: x.split(',')[2]) # sort by party affiliation
partyAffiliation = listx[2]
rep = []
dem = []
ind = []
numRep = []
numDen = []
numInd = []
while (partyAffiliation == 'R'):
rep = rep.expend
numRep == int(len(rep))
while (partyAffiliation == 'D'):
dem = dem.extend
numDem == int(len(dem))
while (partyAffiliation == 'I',):
ind = ind.extend
numInd == int(len(ind))
print('Party Affiliation: ')
print('Republicans: ',numRep)
print('Democrats:' ,numDem)
print('Independents:' ,numInd)
main()
假设数据与此相似:
您可以使用如下代码:
输出
您可以添加:
相关问题 更多 >
编程相关推荐