Skip to content

Instantly share code, notes, and snippets.

@Caleb2501
Created May 17, 2013 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caleb2501/5596876 to your computer and use it in GitHub Desktop.
Save Caleb2501/5596876 to your computer and use it in GitHub Desktop.
This challenge 25 was a lot of fun. I added some additional statistics to the end of the output to make it interesting.
# In an election, the person with the majority of the votes is the winner. Sometimes
# due to similar number of votes, there are no winners.
# Your challenge is to write a program that determines the winner of a vote,
# or shows that there are no winners due to a lack of majority.
import random
def vote(team1, team2, times):
"""Takes two strings and an int, randomly votes for each of the strings
then gives the string that gained the most votes.
it returns a tally int that indicates which team won over all"""
tally1 = 0
tally2 = 0
for i in range(1, (times + 1)):
team1vote = random.randint(20, 100)
team2vote = random.randint(20, 100)
print "+++++++++++++++++++++++Round %d+++++++++++++++++++++++++++++" % i
print team1, " scored %d votes." %team1vote
print team2, " scored %d votes." %team2vote
if team1vote == team2vote:
print "There were no winners, try again"
elif team1vote > team2vote:
print "%s won the majority of the votes! We have a winner!" % team1
tally1 += 1
else:
print "%s won the majority of the votes! We have a winner!" % team2
tally2 +=1
return tally1, tally2
blue = raw_input("Give me a name for the first team: ")
red = raw_input("Give me a name for the second team: ")
elections = input("How many elections do you want to hold? ")
print "Let's get started!"
totalBlue, totalRed = vote(blue, red, elections)
# Evaluate and assign the winner
if totalBlue > totalRed:
total = totalBlue
winner = blue
elif totalRed > totalBlue:
total = totalRed
winner = red
else:
winner = "Neutral"
total = int(elections * .5)
print "The results of %d elections show that over all Team %s was the best!" % (elections, winner)
if elections % 2 == 0:
halfElect = elections * .5
else:
halfElect = (elections + 1) * .5
percentage = ((1.0 * total) / elections) * 100
print winner, "won %d percent of the time!" % percentage
print total, "times total!"
if (totalBlue + totalRed) != elections:
zeroes = elections - (totalBlue + totalRed)
print "Also there were %d elections that ended in a tie." % zeroes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment