Skip to content

Instantly share code, notes, and snippets.

@robb-romans
Last active December 21, 2023 15:26
Show Gist options
  • Save robb-romans/d6a76b141ca93eb2524f2571bd27fe09 to your computer and use it in GitHub Desktop.
Save robb-romans/d6a76b141ca93eb2524f2571bd27fe09 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# ./wtd-winner.py
import random
def determine_winner(num1, num2):
"""
Function to determine a winner between two users guessing a number between 1 and 100, with the
winner being the closest guess to a generated random number.
Parameters:
num1 (int): First number in the range 1 to 100
num2 (int): Second number in the range 1 to 100
num1 cannot equal num2
Returns: str: A message indicating the random number and the closest input number
"""
if 1 <= num1 <= 100 and 1 <= num2 <= 100 and num1 != num2:
# Pick a random number
random_number = random.randint(1, 100)
# Determine the closest number
closest = min([num1, num2], key=lambda x: abs(x - random_number))
return f"Random number: {random_number}. Closest input: {closest}"
else:
return "Input numbers must be unique and in the range of 1 to 100."
def main():
try:
# Get input from the user
num1 = int(input("Enter the first number (1-100): "))
num2 = int(input("Enter the second number (1-100): "))
# Call the function and display the result
result = determine_winner(num1, num2)
print(result)
except ValueError as ve:
print(ve)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment