I'm currently working on a school project and I'm have difficulties on the very end of my program.
def index_district():
dist = input("Please enter district: ")
district=[dist]
candidates = []
var1 = 1
while var1:
var1 += 1
candidates_names = input('Enter name of candidate for the district. Hit enter again to end ')
if candidates_names == '':
break
candidates.append(candidates_names)
votes = []
var2 = 1
while var2:
var2 += 1
candidates_votes = input('Enter number of votes for the district. Hit enter again to end ')
if candidates_votes == '':
break
votes.append(candidates_votes)
parties = []
var3 = 1
while var3:
var3 += 1
candidates_parties = input('Enter parties for the district. Hit enter again to end ')
if candidates_parties == '':
break
parties.append(candidates_parties)
canidate_parties = dict(zip(parties, candidates))
party_votes = dict(zip(parties, votes))
dic = dict(zip(district, zip(canidate_parties, party_votes)))
print(dic)
index_district()
And my output is
Please enter district: qwe
Enter name of candidate for the district. Hit enter again to end as
Enter name of candidate for the district. Hit enter again to end ds
Enter name of candidate for the district. Hit enter again to end xc
Enter name of candidate for the district. Hit enter again to end
Enter number of votes for the district. Hit enter again to end 123
Enter number of votes for the district. Hit enter again to end 234
Enter number of votes for the district. Hit enter again to end 345
Enter number of votes for the district. Hit enter again to end
Enter parties for the district. Hit enter again to end rrr
Enter parties for the district. Hit enter again to end eee
Enter parties for the district. Hit enter again to end www
Enter parties for the district. Hit enter again to end
{'qwe': ('rrr', 'rrr')}
Process finished with exit code 0
however my end code is supposed to look something like this (Im using the example provided by the prof):
{’Name ’: ’Humboldt ’,
’Candidates ’: {’LIB ’: ’Elliott ’, ’NDP ’: ’Angela ’, ’SK ’: ’Mr. Robot ’},
’Votes ’: {’LIB ’: 2732 , ’NDP ’: 101 , ’SK ’: 370}
}
What im not understanding is how to make the lists work in the fashion i want them to. Any help would be appreciated.
A smaller part of the program works fine:
def create_mapping():
keys = []
i = 0
while 1:
i += 1
item = input('Enter party. Hit enter again to end ' )
if item == '':
break
keys.append(item)
votes = []
x = 0
while 1:
x += 1
partyvotes = input('Enter number of votes. Hit enter again to end ')
if partyvotes == '':
break
votes.append(partyvotes)
dic = dict(zip(keys, votes))
length = len(keys)
print (dic)
create_mapping()
which gives me the output of
Enter party. Hit enter again to end red
Enter party. Hit enter again to end blue
Enter party. Hit enter again to end green
Enter party. Hit enter again to end
Enter number of votes. Hit enter again to end 123
Enter number of votes. Hit enter again to end 234
Enter number of votes. Hit enter again to end 345
Enter number of votes. Hit enter again to end
{'red': '123', 'blue': '234', 'green': '345'}
But why isnt that going over as well in the bigger program?
It is not error. Just you did not use proper method to generate your output.
I'm suggesting you follow your code line by line:
canidate_parties = dict(zip(parties, candidates))
# => {'rrr': 'as', 'eee': 'ds', 'www': 'xc'}
party_votes = dict(zip(parties, votes))
# => {'rrr': 123, 'eee': 234, 'www': 345}
# This is wrong part
dic = dict(zip(district, zip(canidate_parties, party_votes)))
# zip(canidate_parties, party_votes)
# => [('rrr', 'rrr'), ('eee', 'eee'), ('www', 'www')]
# zip(district, zip(canidate_parties, party_votes)))
# => [('qwe', ('rrr', 'rrr'))]
# dict(zip(district, zip(canidate_parties, party_votes))))
# => {'qwe': ('rrr', 'rrr')}
I think you can get a point. Learning more about dict()
and zip()
will help you. If you want your output, you need to change dic = ...
line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.