import random
def generate_code():
"""Generate a random three-digit code."""
return str(random.randint(100, 999))
def check_guess(code, guess):
"""Check the guess against the code and return the result."""
if code == guess:
return "Congratulations! You cracked the code!"
else:
matching_digits = sum(1 for x, y in zip(code, guess) if x == y)
return f"Wrong guess. {matching_digits} digit(s) are correct."
def main():
print("Welcome to the Code-Cracking Game!")
print("A three-digit code has been generated. Try to crack it!")
secret_code = generate_code()
while True:
user_guess = input("Enter your guess (a three-digit number): ")
if not user_guess.isdigit() or len(user_guess) != 3:
print("Please enter a valid three-digit number.")
continue
result = check_guess(secret_code, user_guess)
print(result)
if result.startswith("Congratulations"):
break
if __name__ == "__main__":
main()