Question: Please give me feedback on how I can possibly improve this Python code I created for Codebreaker, a modified version of the game Mastermind. No

Please give me feedback on how I can possibly improve this Python code I created for "Codebreaker", a modified version of the game "Mastermind". No AI please!
import random
import time
# Function to generate a random 4-digit code with numbers from 1-8(no repeats)
def generate_secret_code():
digits = list(range(1,9))
random.shuffle(digits)
secret_code = digits[:4]
return secret_code
# Function to check if the player's guess is valid
def is_valid_guess(guess):
if len(guess)!=4:
print("Oops! The guess should be exactly 4 digits long. Try again.")
return False
if not all(char.isdigit() and 1<= int(char)<=8 for char in guess):
print("Invalid input! Only digits from 1 to 8 are allowed.")
return False
if len(set(guess))!=4:
print("No repeats! The digits in your guess must be unique.")
return False
return True
# Function to calculate X (correct digit & position) and O (correct digit, wrong position)
def evaluate_guess(secret_code, guess):
X = sum([1 for i in range(4) if secret_code[i]== int(guess[i])])
O = sum([1 for i in range(4) if int(guess[i]) in secret_code and secret_code[i]!= int(guess[i])])
return X, O
# Game introduction and rules
def intro():
print("Welcome to Codebreaker!")
print("Your goal: Crack the secret 4-digit code using numbers from 1 to 8. No repeats!")
print("For each guess, you'll get 'X'(correct digit in the right place) and 'O'(correct digit, wrong place).")
print("You have 10 attempts to crack the code. Good luck!")
# Main game loop
def play_game():
secret_code = generate_secret_code()
guessed_codes =[] # List to store previously guessed codes
attempts =10
intro()
while attempts >0:
guess = input(f"
Enter your guess (4 digits, digits 1-8): ")
# Check if the guess is valid
if not is_valid_guess(guess):
continue
# Check if the guess has been used before
if guess in guessed_codes:
print("You've already guessed that combination! Try something new.")
continue
# Record the guess
guessed_codes.append(guess)
# Evaluate the guess
X, O = evaluate_guess(secret_code, guess)
# Show the results
print(f"Guess: {guess}-> X: {X} O: {O}| Attempts Left: {attempts -1}")
time.sleep(1) # Add a brief pause for suspense
if X ==4:
print(f"
Congratulations! You've cracked the code {secret_code}!")
break
attempts -=1
if attempts ==0 and X !=4:
print(f"
Sorry! You've used up all your attempts. The correct code was: {secret_code}. Better luck next time!")
# Ask if the user wants to play again
replay = input("
Would you like to play again? (yes/no): ").lower()
if replay == "yes":
play_game() # Recursively restart the game
else:
print("Thanks for playing Codebreaker! Goodbye!")
# Start the game
if __name__=="__main__":
play_game()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!