Skip to content

Instantly share code, notes, and snippets.

@tliesnham
Last active July 30, 2018 15:00
Show Gist options
  • Save tliesnham/fea9316bebce8c23bba1eb29e9778ada to your computer and use it in GitHub Desktop.
Save tliesnham/fea9316bebce8c23bba1eb29e9778ada to your computer and use it in GitHub Desktop.
A Python based guess the number game where the computer attempts to guess the user's number based on past input.
import random
rounds = 0
guesses = 0
numbers = []
print("Welcome to Guess! Each round a player picks a number between 1 and 10. The computer then attempts to guess this number.\n")
def success_rate():
if guesses > 0:
return (guesses / (rounds-10)) * 100
return 0
while True:
players_number = int(input("Enter a number between 1 and 10: "))
numbers.append(players_number)
rounds += 1
if rounds > 10:
random.shuffle(numbers)
guess = numbers[len(numbers)-1]
print("Computer guesses: {}\n".format(guess))
if guess == players_number:
guesses += 1
print("Success rate: {}%".format(success_rate()))
@tliesnham
Copy link
Author

tliesnham commented Jul 27, 2018

Based on the user inputting random numbers this would return a theoretical win rate of 10%. Anything over 10% would suggest that the numbers are not random.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment