Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Elections program!!
01-25-2010, 10:56 AM
Post: #1
Thumbs Up [Python] Elections program!!
Okay this is one interesting task which I got on my studies, for practice, few weeks before my second Colloqua exam. I find it very interesting so I will share it. I will put few comments in some places for better understanding... I hope. Smile

Code:
no_candidates=int(raw_input("Enter the number of candidates: "))
#Make an empty list of candidates, then enter the candidates one by one and
#add them to list
candidates=[]
for i in range(no_candidates):
    name=raw_input("Enter name of candidate %d: "%(i+1))
    candidates.append(name)
for i,name in enumerate(candidates):
    print '%d. %s'%(i+1,name)
#For every canidate we will make a "vote counter". In the beginning all
#counters are 0. We load votes one by one, and check if the vote is legible.
#We check if the entered number is -1 because that number ends the voting.
#Then we increase the appropriate counter.
votes=[0]*no_candidates
while True:
    vote=int(raw_input("Enter vote(-1 to end voting): "))
    if vote!=-1 and not (1<=vote<=no_candidates):
        print "Invalid vote!"
        continue
    if vote==-1:
        break
    votes[vote-1]+=1
#Print the candidate with the same index as the maximum list of votes.
max_index=votes.index(max(votes))
print "Winner is: %s"%candidates[max_index]
#For every candidate print name and the precentage of votes.
sum_all_votes=sum(votes)
for i in range(no_candidates):
    print '%s won %.2f%% of votes'%(candidates[i],100.0*votes[i]/sum_all_votes)
Ask if you need some clearing out about the code, and here is the attachment. I suggest that you download it and read it because I don't know how readable will it be in "quotes".

.rar  elections.rar (Size: 672 bytes / Downloads: 4)


Oh I almost forgot. Here is an example how it looks in Python Shell after you run it.
Code:
Enter the number of candidates: 3
Enter name of candidate 1: Mike
Enter name of candidate 2: Nick
Enter name of candidate 3: Tyler
1. Mike
2. Nick
3. Tyler
Enter vote(-1 to end voting): 1
Enter vote(-1 to end voting): 2
Enter vote(-1 to end voting): 3
Enter vote(-1 to end voting): 4
Invalid vote!
Enter vote(-1 to end voting): 2
Enter vote(-1 to end voting): 2
Enter vote(-1 to end voting): 2
Enter vote(-1 to end voting): 1
Enter vote(-1 to end voting): 1
Enter vote(-1 to end voting): 3
Enter vote(-1 to end voting): 3
Enter vote(-1 to end voting): 3
Enter vote(-1 to end voting): 3
Enter vote(-1 to end voting): -1
Winner is: Tyler
Mike won 25.00% of votes
Nick won 33.33% of votes
Tyler won 41.67% of votes
I will post some more tasks after I do them. Tongue Blush
Find all posts by this user
Quote this message in a reply
01-25-2010, 12:12 PM
Post: #2
RE: [Python] Elections program!!
Nice I'm ready to hack you voting system. JK Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
01-25-2010, 08:16 PM
Post: #3
RE: [Python] Elections program!!
Good use of loops. Good calculations and fast algorithm. Overall very good !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-26-2010, 04:41 AM
Post: #4
RE: [Python] Elections program!!
lol i will sell it in the next US president elections. i will be rich.... lol

@co(de)caine Tongue
well im just a beginner and i dont know how to protect programs, and my intention wasnt to protect it. i shared the source Smile

@drdebcol
thx bro Smile
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: