Create a guessing game | AI-created blog with Ainan Kuma Farm

AI-created blog with Ainan Kuma Farm

This blog is created with AI and Ainan Kuma Farm.
Articles here MAY NOT BE based on my personal or official ideas.

Prompt:
--------
Please create a Python program for a guessing game with the following rules:

The game involves guessing a 3-digit number.
All three digits must be unique (no repeats).
The program will generate a random 3-digit number to be guessed.
The user keeps guessing until they guess the correct number.
After each guess, the program provides feedback in the format "n strikes m balls".
A "strike" means a digit is correct and in the right position.
A "ball" means a digit is correct but in the wrong position.
For example:
If the secret number is 432 and the guess is 402, the feedback is "1 strike 1 ball".
If the secret number is 312 and the guess is 321, the feedback is "1 strike 2 balls".
When the user guesses the correct number, the program should display a message like "Congratulations! You guessed it!" and end the game.
--------

Response by Google Gemini
--------
Python 3-Digit Guessing Game

Python
import random

# Generate a random 3-digit number
secret_num = [str(random.randint(0, 9)) for _ in range(3)]

# Game loop
while True:
  # Get user input as a list of digits
  user_num = [str(num) for num in input("Enter a 3-digit number: ")]

  # Count strikes and balls
  strike = 0
  ball = 0
  for i in range(3):
    if user_num[i] == secret_num[i]:
      strike += 1
    elif user_num[i] in secret_num:
      ball += 1

  # Display game result
  print(f"{strike} strikes {ball} balls")

  # Check if the guess is correct
  if strike == 3:
    print("Congratulations! You guessed it!")
    break

Use code with caution.
content_copy
Explanation:

We import the random module to generate random numbers.
The secret_num list stores the randomly generated 3-digit number as strings.
The game loop keeps running until the user guesses the correct number.
We get the user's guess as a 3-digit number and convert it to a list of strings.
strike and ball variables are initialized to 0.
A for loop compares each digit of the guess with the secret number.
If a digit matches in the same position, strike increases by 1.
If a digit exists in the secret number but in a different position, ball increases by 1.
The loop displays the feedback based on the number of strikes and balls.
If strike reaches 3, it means the user guessed the correct number, and the program congratulates the user and ends the loop.
Additional Notes:

This program provides basic functionality.
You can enhance it by adding features like:
Setting a limit on the number of guesses.
Including a hint system.
Keeping track of past guesses.
The code is written for better readability for beginners.
I hope this translation is helpful!

--------
Created with Google Gemini / Picture created with Microsoft Copilot

Note:
Gemini generated a beginner friendly source code. English version works fine, but Japanese version has some bags.
ChatGPT and Microsoft Copilot generated more sophisticated sources work fine in both languages.

日本語版はこちら

https://blog.kuma-farm-japan.jp/