Link to home
Start Free TrialLog in
Avatar of L Ward
L Ward

asked on

Python formatting and value output.

Hello! Having trouble with my python code. Two issues-
One- How can I format this nicely, so that the values of the dictionary line up under the header? I was able to get all the keys to print on separate lines but that's it.
Two- my loop that is supposed to give me the candidate with the most votes keeps pulling in the candidate with the highest key (the last input). Thanks in advance for any help!
Here is an example of the output I want
ID Name    Votes   %of Votes
1   Homer   90          90%
2   Dan        10         10%
and the winner is Dan!

totalVotes=[]
dct = {}
i = 1
while(True):
    name = input('Please enter a name: ')
    if name == '':

        break
    votes = input('Please enter vote total for canidate: ')
    totalVotes.append(votes)
    totalVotesInt= map(int, totalVotes)
    total = sum(totalVotesInt)
    dct[i] = list((name,int(votes)))   
    i += 1

maxVal = 0
for i in range(1, len(dct) + 1):
    if dct[i][1] > maxVal:
        maxInd = i
    dct[i].append(int((dct[i][len(dct[i]) - 1]) / total * 100))

header='{:>0}{:>10}{:>10}{:>20}'.format('ID','Name','Votes','% of Total Vote')
print(header)    
print1=("\n".join("{}\t{}".format(key, value) for key, value in dct.items()))
print(print2)
print('Total '+str(total))
print('The Winner of the Election is '+ dct[maxInd][0])

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of L Ward
L Ward

ASKER

Thank you, you're a life saver!