# A guessing game where the computer thinks of a # 2-digit number and the user tries to guess it. # Two-digit number-guessing game with hinting. import random # Reports a hint about how many digits from the given # guess match digits from the given correct number. def matches(number, guess): num_matches = 0 if guess // 10 == number // 10 or guess // 10 == number % 10: num_matches += 1 if guess // 10 != guess % 10 and (guess % 10 == number // 10 or guess % 10 == number % 10): num_matches += 1 return num_matches def main(): print("Try to guess my two-digit number, and I'll tell you") print("how many digits from your guess appear in my number.") print() # pick a random number from 0 to 99 inclusive number = random.randint(0, 99) # get first guess guess = int(input("Your guess? ")) num_guesses = 1 # give hints until correct guess is reached while guess != number: num_matches = matches(number, guess) print("Incorrect (hint:", num_matches, "digits match)") guess = int(input("Your guess? ")) num_guesses += 1 print("You got it right in", num_guesses, "tries.") main()