Question: The code master game is to crack 4 digits code. The program saves a 4 digit code and allows users to guess every time. Each

The code master game is to crack 4 digits code. The program saves a 4 digit code and allows users to guess every time.
Each round, the program will show the result of the guessing for users to interpret the correct code.
How to determine number of As and Bs:
If the guess number's position and value are both correct, the program counts A.
How many A will be calculated based on how many digits have correct positions and values.
If the guess number only has correct value, the program counts B.
How many B will be calculated based on how many digits have only correct values.
Therefore, there is no 4A since 4A means the user makes the exact right guess (breaking the code).
For example, if the secret code is 1234,
when the user guess 5678, the program shows 0 A and 0 B.when the user guess 5674, the program shows 1 A and 0 B.when the user guess 4678, the program shows 0 A and 1 B.when the user guess 1274, the program shows 3 A and 0 B.
NOTE:one important game rule for this is that the program does not allow any repeated number in any of two digits for the code.
For example, 1122 or 1123 or 1222 are NOT allowed.
Description:
Now, you need to write the program that can allow user to play the game and then stop when the user's guess match the secret code (master code).
Todo function:
You need to create the following functions. These functions will help you to create the game program.
check_repeat_code()
The function receives a parameter - a code (string) and return True or False. If the code has repeat digits, the function need to return True, otherwise, return False. So, inside the function, you need a loop to detect repeat digits.
determine_ab(,)
This function will count how many As and Bs by comparing two strings (your_guess_code and program_generated_code). Then, return two integers: number of As and number of Bs.
Given function:
generate_master_code(n=4)
This function uses random function to generate non-repeated codes, the default number of digits is 4, so, without passing n, the function will return a string with 4 digits code, for example, 1425, and return the code string.
Main part of the program:
Your main part will deal with the whole program execution. Therefore, it will contain the following elements:
You need a loop for users to guess again and again.
You do not allow user to enter a guessing code with repeated digits.
Hints:
Suggested to start from check_repeat_code() and determine_ab().
Print out the generated secret code for playing and user testing.
Grading:
Comments (3pts)
Passing Tests (9pts)
Proper Coding (3pts)
Execution Example:
```
Game Start!
Please enter your guess
1123
You have repeated digits. Please enter your guess again
1234
A: 0 B: 1
Not match all! Please try again
Please enter your guess
5678
A: 1 B: 1
Not match all! Please try again
Please enter your guess
3579
A: 1 B: 2
Not match all! Please try again
Please enter your guess
3598
A: 2 B: 2
Not match all! Please try again
Please enter your guess
3958
A: 4 B: 0
You win!
```
import random
def generate_master_code(n=4):
pool = list("0123456789")
if n >10:
print("error. exceed the max")
return
code =[]
code = random.sample(pool, n)
codeStr ="".join(code)
return codeStr
def check_repeat_code(code):
#put your codes below
def determine_ab(master, guess):
#put your codes below
if __name__=="__main__":
print("Game Start!")
# The master is the code for a user to guess
# Please check the return value of the function
# to understand the datatype of the master code
master = generate_master_code()
# print(master)
# Put your code below

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!