#Knight Scott
import random
#Create class GUESSWHAT ( The game )
class GuessWhat:
#each instance of the game will need to know a range , starting and ending
def __init__(self,starting,ending):
self.starting = starting
self.ending = ending
# a function(method) that allows the instance to find a random target number within the range created by the instance
def get_secret_number(self):
global secret_number
starting = self.starting
ending= self.ending
secret_number = random.randrange(starting,ending,1)
# UNCOMMENT THIS TO SEE THE SECRET NUMBER
#print(secret_number)
return secret_number
# this method prompts the user for the guess and tries to validate the choice
def get_user_choice(self):
global user_choice
starting = self.starting
ending= self.ending
user_choice = input('Enter a number between %s and %s :' % (starting,ending))
# "the class should ensure that the numbers entered by the user are not strings"
# this strips out the spaces and returns true if its a number , false if it's not
if user_choice.strip().isdigit() == False:
print('Your choice is not a number or is invalid .... try again\n')
self.get_user_choice()
# the class should ensure that the numbers entered by the user are between the range values
if int(user_choice) < starting or int(user_choice) > ending:
print('Your choice is not in range :( Please try again \n')
self.get_user_choice()
else:
return int(user_choice)
#This method checked the user guess vs the secret number and prompts them whether it is higher or lower, if its a wrong guess it adds one to wrong guess to tell them later.
def compare_choices(self):
global wrong_guesses
wrong_guesses = 1
while user_choice != secret_number:
if int(user_choice) > secret_number:
print('Your number is higher then the number I guessed, Try again\n')
wrong_guesses = wrong_guesses + 1
self.get_user_choice()
elif int(user_choice) < secret_number: # having problem here
print('Your number is lower then the number I guessed, Try again\n')
wrong_guesses = wrong_guesses + 1
self.get_user_choice()
else:
print('You guessed it correctly! it took you ', wrong_guesses , ' tries')
break
#This creates a method PLAY GAME to combine all the components that make up the game
def play_game(self):
self.get_user_choice()
self.get_secret_number()
self.compare_choices()
#Creates the instance of the game , Can plug in the range, then calls the game
Game = GuessWhat(1,100)
Game.play_game()
I guess the “input” method doesn’t work in codebyte ?