from random import randint """ plays a single game of Master Mind, where the computer picks a number and the human guesses """ def play_computer_picks(): number_of_guesses = 0 pick = pick_number() while True: guess = get_guess_from_user() number_of_guesses = number_of_guesses + 1 if pick == guess: print "Congratulations!!! You found the answer in", number_of_guesses, "times." break if pick.lower() == "q": print "better luck next time!" print "the answer was", pick break feedback = compute_answer(pick, guess) print "Feedback:", feedback """ returns a random string, containing four digits between 1 and 6 """ def pick_number(): pick = "" for i in range(0, 4): digit = randint(1, 6) pick = pick + str(digit) return pick """ returns a string entered by the user, containing four digits between 1 and 6. We also allow a "q" or "Q". Anything else is rejected and we ask the user to try again. """ def get_guess_from_user(): while True: guess = raw_input("enter your guess: ") if (guess.lower() == "q"): return guess if (is_valid_guess(guess)): return guess print guess, "is not a valid guess, try again." """ returns True if the guess is a four-digit string containing digits from 1 to 6. """ def is_valid_guess(guess): for character in guess: if not(character in "123456"): return False return True print pick_number()