Skip to content

Instantly share code, notes, and snippets.

@DarthJahus
Last active August 29, 2015 14:13
Show Gist options
  • Save DarthJahus/9dc1f35e9157ea4d9940 to your computer and use it in GitHub Desktop.
Save DarthJahus/9dc1f35e9157ea4d9940 to your computer and use it in GitHub Desktop.
Random number guessing game, the closer wins (part)
import random
Between = {"min": 1, "max": 5} # to fit the "human" message
print("Please, guess the number between %(min)i and %(max)i" % Between)
# Getting the random winning number
WinningNumber = random.randrange(Between.get("min") - 1, Between.get("max")) + 1
print("Winning number is %s" % WinningNumber)
# Getting the votes
Votes = {1: "ItsLuke", 2: "vbstrdg", 12: "Jahus"}
print("Actual votes are: %s" % Votes)
# adding a vote
guess = 3; user = "Dj4x"
# checking if the vote exists
if guess not in Votes:
Votes.update([(guess, user)])
print("Votes updated: %s" % Votes)
else:
print("--NOTIFY--(%s) %s has already voted with %i" % (user, Votes.get(guess), guess))
#
# Getting the winner :
distance = {abs(vote - WinningNumber): Votes.get(vote) for vote in Votes}
print("distance: %s" % distance)
print("Winner is %s" % distance.get(sorted(distance)[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment