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 digits code. The program saves a 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 A since A means the user makes the exact right guess breaking the code
For example, if the secret code is
when the user guess the program shows A and Bwhen the user guess the program shows A and Bwhen the user guess the program shows A and Bwhen the user guess the program shows A and 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, or or 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.
checkrepeatcode
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.
determineab
This function will count how many As and Bs by comparing two strings yourguesscode and programgeneratedcode Then, return two integers: number of As and number of Bs
Given function:
generatemastercoden
This function uses random function to generate nonrepeated codes, the default number of digits is so without passing n the function will return a string with digits code, for example, 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 checkrepeatcode and determineab
Print out the generated secret code for playing and user testing.
Grading:
Comments pts
Passing Tests pts
Proper Coding pts
Execution Example:
Game Start!
Please enter your guess
You have repeated digits. Please enter your guess again
A: B:
Not match all! Please try again
Please enter your guess
A: B:
Not match all! Please try again
Please enter your guess
A: B:
Not match all! Please try again
Please enter your guess
A: B:
Not match all! Please try again
Please enter your guess
A: B:
You win!
import random
def generatemastercoden:
pool list
if n :
printerror exceed the max"
return
code
code random.samplepool n
codeStr joincode
return codeStr
def checkrepeatcodecode:
#put your codes below
def determineabmaster guess:
#put your codes below
if namemain:
printGame 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 generatemastercode
# printmaster
# Put your code below
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
