Last active
August 3, 2018 13:45
-
-
Save tliesnham/1f2e370232653b96a5d3aa1600a94a88 to your computer and use it in GitHub Desktop.
A Python based guessing game that attempts to predict the user's number based on past numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Guess: | |
correct_guesses = 0 | |
def next_number(self, number): | |
"""Return the number most likely to be picked by the human. | |
If no candidates are found, return a random number.""" | |
potential_numbers = [] | |
for index,num in enumerate(self.numbers[:-2]): | |
if number == num and index + 1 < len(self.numbers): | |
potential_numbers.append(self.numbers[index+1]) | |
if len(potential_numbers) > 0: | |
random.shuffle(potential_numbers) | |
return potential_numbers[-1] | |
return random.randint(1,10) | |
def win_rate(self): | |
"""Return win rate as a percentage.""" | |
if self.correct_guesses > 0: | |
return (self.correct_guesses / (self.rounds - self.training_rounds)) * 100 | |
return 0 | |
def __init__(self, numbers = [], training_rounds = 10): | |
self.numbers = numbers | |
self.training_rounds = training_rounds | |
print("Welcome to Guess! Each round a player picks a number between 1 and 10.\nThe computer then attempts to guess this number.\n") | |
while True: | |
self.numbers.append(int(input("Enter a number between 1 and 10: "))) | |
self.rounds = len(numbers) | |
if self.rounds > self.training_rounds: | |
computers_number = self.next_number(self.numbers[-2]) | |
print(computers_number) | |
if computers_number == self.numbers[-1]: | |
self.correct_guesses += 1 | |
print("Win rate: {}%".format(self.win_rate())) | |
Guess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment