Skip to main content
Tweeted twitter.com/StackCodeReview/status/1078349960930836480
Missing details added
Source Link

Bulls and Cows is a simple code-breaking game sometimes better known as "Mastermind." The rules are explained here: https://en.wikipedia.org/wiki/Bulls_and_Cows

I would appreciate some feedback on the following codeimplementation in terms of overall code quality, layout and logic. It's meant to be at a fairly basic level so I don't want to over complicate things, but want to avoid any obviously bad choices. Suggestions for improvements very welcome.

import random

SECRET_NUMBER_SIZE = 2
MAX_GUESSES = 5

# Generate secret number with SECRET_NUMBER_SIZE digits and no duplicates
secret_number = ""
while len(secret_number) < SECRET_NUMBER_SIZE:
    new_digit = str(random.randint(0, 9))
    if new_digit not in secret_number:
        secret_number += new_digit

# print(f"For testing. Secret number is: {secret_number}")

print(f"Guess my number. It contains {SECRET_NUMBER_SIZE}\
unique digits from 0-9")

remaining_turns = MAX_GUESSES

while remaining_turns <= MAX_GUESSES:

    # Get user guess and validate length
    player_guess = input("Please enter your guess: ")
    if len(player_guess) != SECRET_NUMBER_SIZE:
        print(f"Your guess must be {SECRET_NUMBER_SIZE} digits long.")
        continue

    # Main game logic
    if player_guess == secret_number:
        print("Yay, you guessed it!")
        break
    else:
        bulls = 0
        cows = 0
        for i in range(SECRET_NUMBER_SIZE):
            if player_guess[i] == secret_number[i]:
                bulls += 1
        for j in range(SECRET_NUMBER_SIZE):
            if player_guess[j] in secret_number and \
              player_guess[j] != secret_number[j]:
                cows += 1
        print(f"Bulls: {bulls}")
        print(f"Cows: {cows}")
        remaining_turns -= 1
        if remaining_turns < 1:
            print("You lost the game.")
            break

I would appreciate some feedback on the following code in terms of overall code quality, layout and logic. It's meant to be at a fairly basic level so I don't want to over complicate things, but want to avoid any obviously bad choices. Suggestions for improvements very welcome.

import random

SECRET_NUMBER_SIZE = 2
MAX_GUESSES = 5

# Generate secret number with SECRET_NUMBER_SIZE digits and no duplicates
secret_number = ""
while len(secret_number) < SECRET_NUMBER_SIZE:
    new_digit = str(random.randint(0, 9))
    if new_digit not in secret_number:
        secret_number += new_digit

# print(f"For testing. Secret number is: {secret_number}")

print(f"Guess my number. It contains {SECRET_NUMBER_SIZE}\
unique digits from 0-9")

remaining_turns = MAX_GUESSES

while remaining_turns <= MAX_GUESSES:

    # Get user guess and validate length
    player_guess = input("Please enter your guess: ")
    if len(player_guess) != SECRET_NUMBER_SIZE:
        print(f"Your guess must be {SECRET_NUMBER_SIZE} digits long.")
        continue

    # Main game logic
    if player_guess == secret_number:
        print("Yay, you guessed it!")
        break
    else:
        bulls = 0
        cows = 0
        for i in range(SECRET_NUMBER_SIZE):
            if player_guess[i] == secret_number[i]:
                bulls += 1
        for j in range(SECRET_NUMBER_SIZE):
            if player_guess[j] in secret_number and \
              player_guess[j] != secret_number[j]:
                cows += 1
        print(f"Bulls: {bulls}")
        print(f"Cows: {cows}")
        remaining_turns -= 1
        if remaining_turns < 1:
            print("You lost the game.")
            break

Bulls and Cows is a simple code-breaking game sometimes better known as "Mastermind." The rules are explained here: https://en.wikipedia.org/wiki/Bulls_and_Cows

I would appreciate some feedback on the following implementation in terms of overall code quality, layout and logic. It's meant to be at a fairly basic level so I don't want to over complicate things, but want to avoid any obviously bad choices. Suggestions for improvements very welcome.

import random

SECRET_NUMBER_SIZE = 2
MAX_GUESSES = 5

# Generate secret number with SECRET_NUMBER_SIZE digits and no duplicates
secret_number = ""
while len(secret_number) < SECRET_NUMBER_SIZE:
    new_digit = str(random.randint(0, 9))
    if new_digit not in secret_number:
        secret_number += new_digit

# print(f"For testing. Secret number is: {secret_number}")

print(f"Guess my number. It contains {SECRET_NUMBER_SIZE}\
unique digits from 0-9")

remaining_turns = MAX_GUESSES

while remaining_turns <= MAX_GUESSES:

    # Get user guess and validate length
    player_guess = input("Please enter your guess: ")
    if len(player_guess) != SECRET_NUMBER_SIZE:
        print(f"Your guess must be {SECRET_NUMBER_SIZE} digits long.")
        continue

    # Main game logic
    if player_guess == secret_number:
        print("Yay, you guessed it!")
        break
    else:
        bulls = 0
        cows = 0
        for i in range(SECRET_NUMBER_SIZE):
            if player_guess[i] == secret_number[i]:
                bulls += 1
        for j in range(SECRET_NUMBER_SIZE):
            if player_guess[j] in secret_number and \
              player_guess[j] != secret_number[j]:
                cows += 1
        print(f"Bulls: {bulls}")
        print(f"Cows: {cows}")
        remaining_turns -= 1
        if remaining_turns < 1:
            print("You lost the game.")
            break
Source Link

Python Bulls and Cows

I would appreciate some feedback on the following code in terms of overall code quality, layout and logic. It's meant to be at a fairly basic level so I don't want to over complicate things, but want to avoid any obviously bad choices. Suggestions for improvements very welcome.

import random

SECRET_NUMBER_SIZE = 2
MAX_GUESSES = 5

# Generate secret number with SECRET_NUMBER_SIZE digits and no duplicates
secret_number = ""
while len(secret_number) < SECRET_NUMBER_SIZE:
    new_digit = str(random.randint(0, 9))
    if new_digit not in secret_number:
        secret_number += new_digit

# print(f"For testing. Secret number is: {secret_number}")

print(f"Guess my number. It contains {SECRET_NUMBER_SIZE}\
unique digits from 0-9")

remaining_turns = MAX_GUESSES

while remaining_turns <= MAX_GUESSES:

    # Get user guess and validate length
    player_guess = input("Please enter your guess: ")
    if len(player_guess) != SECRET_NUMBER_SIZE:
        print(f"Your guess must be {SECRET_NUMBER_SIZE} digits long.")
        continue

    # Main game logic
    if player_guess == secret_number:
        print("Yay, you guessed it!")
        break
    else:
        bulls = 0
        cows = 0
        for i in range(SECRET_NUMBER_SIZE):
            if player_guess[i] == secret_number[i]:
                bulls += 1
        for j in range(SECRET_NUMBER_SIZE):
            if player_guess[j] in secret_number and \
              player_guess[j] != secret_number[j]:
                cows += 1
        print(f"Bulls: {bulls}")
        print(f"Cows: {cows}")
        remaining_turns -= 1
        if remaining_turns < 1:
            print("You lost the game.")
            break