110k views
2 votes
Pair programming using pair programming, you will create a similar game, but with numbers instead of colors. The game will be written in python. It should operate essentially the same way, but the computer will be the 'code maker' and the user will be the 'code breaker.' Also, instead of telling a user how many are correct and in the right place and how many are correct but in the wrong place, the program should only reveal how many are correct and in the right place. Program specifications:

1. The computer should generate 4 random numbers from 1-9, inclusive, as the 'secret code'.
2. The user should be prompted for their guess of those four numbers.
3. After they provide their full guess, the user is told how many are correct.
4. As long as the user does not get all four correct, they keep getting asked for their guess.
5. After the user finally gets all four numbers correct, they are congratulated and then told how many tries it took them.

Technical requirements:

1. Use at least one list.
2. Use at least one function with parameters.

Click the 'Run Code' button to run the program in the terminal panel.

Run code type 'clear' and press enter in the terminal panel to clear the terminal.

Mastermind Auto-Grader: When you are ready to submit your program to the auto-grader, click the button below.

Check it! Rubric Criteria:

- Program should generate 4 random numbers from 1-9 as the 'secret code'.
- User should be prompted for their guess of those four numbers.
- Program uses at least one list.
- Program defines and calls at least one function with parameters.

User Kumar KL
by
6.7k points

1 Answer

2 votes

Final answer:

In the number-guessing game, Python is used to generate a secret code of four random numbers. The user inputs guesses until the correct code is obtained, with feedback provided for each incorrect guess. The game concludes by congratulating the user and stating the number of attempts made.

Step-by-step explanation:

To create a number-guessing game in Python similar to Mastermind, you will need to complete a series of steps to ensure the game works as specified. Firstly, let's define a function to generate a secret code. This function will use randint from the random module to generate four unique random numbers between 1 and 9. The code will also include a loop to repeatedly prompt the user for their guess and check how many digits are correct and in the correct position.



To start coding, you can define the generate_code function:



import random

def generate_code():
return [random.randint(1, 9) for _ in range(4)]



Next, create the function get_user_guess to prompt the user for a guess and validate the input:



def get_user_guess():
guess = input("Enter your guess (four numbers 1-9): ")
return list(map(int, guess))



Lastly, implement the main loop to run the game:



def main():
secret_code = generate_code()
number_of_tries = 0

while True:
guess = get_user_guess()
number_of_tries += 1
correct_count = sum(gc == gu for gc, gu in zip(secret_code, guess))
if correct_count == 4:
print(f"Congratulations! You guessed the correct code in {number_of_tries} tries!")
break
else:
print(f"{correct_count} digit(s) are correct and in the right place.")

if __name__ == "__main__":
main()



Note: Throughout the game, keep guessing is encouraged until the correct code is entered, after which the user is congratulated and the number of tries is revealed.

User Mallorn
by
7.0k points