import random #generateList #This function takes no input and randomly generates a list of four characters #selected from the list R, G, B, Y. It then returns the list. #input: none #output: a list of four characters def generateList(): #create list of 4 possible letters choices = ['R', 'G', 'B', 'Y'] #create empty solution solution = [] #four times # generate random int to represent position in list of choices # add elt at randomly chosen position in choices to solution for i in range(4): randnum = random.randint(0, 3) solution.append(choices[randnum]) #return new list of characters return solution #testing generateList() #result = generateList() #print result #getGuess #This function takes no input and prompts the user for four characters. #The characters are stored in a list that is then returned. #input: none #output: a list of four characters def getGuess(): #create an empty list fullguess = [] #four times # prompt user for guess for i in range(4): guess = raw_input("Enter the next character of your guess [R, G, B, Y]: ") #validate input while guess != 'R' and guess != 'G' and guess != 'B' and guess != 'Y': guess = raw_input("Bad input. Enter another guess - RGB or Y: ") fullguess.append(guess) return fullguess #testing #userguess = getGuess() #print userguess #compare #This function takes as input two lists and returns True if the lists #contain the same four characters in the same locations and False otherwise. #input: two lists #output: boolean def compare(solution, guess): #loop - four times # compare solution at pos i to guess at pos i for i in range(4): if solution[i] != guess[i]: return False return True #testing #list1 = ['R', 'B', 'B', 'Y'] #list2 = ['R', 'B', 'B', 'G'] #result = compare(list1, list2) #print result #main logic #generate a list of 4 random characters solution = generateList() userinput = 'y' #while the user wants to guess again while userinput == 'y': # get a guess from the user guess = getGuess() # compare the guess to the random list result = compare(solution, guess) # if the user guessed correctly if compare == True: # print a message print "You guessed correctly!" # set guess again to be False userinput = 'n' else: print "Incorrect guess." userinput = raw_input("Would you like to guess again? ") # else # ask if the user wants to guess again