Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have finished a small piece of code for a project called Codebreaker where a number is generated and one has to guess. Your guess is compared to the generated array named "Code". Currently the part I have programmed is where the program outputs the AMOUNT of correct digits in the wrong place within your guess. Is there a way to shorten this or turn it into a sub-routine?

if guess1== code[1]:
    wronPlace= (wronPlace+1)
if guess1== code[2]:
    wronPlace= (wronPlace+1)
if guess1== code[3]:
    wronPlace= (wronPlace+1)

if guess2== code[0]:
    wronPlace= (wronPlace+1)
if guess2== code[2]:
    wronPlace= (wronPlace+1)
if guess2== code[3]:
    wronPlace= (wronPlace+1)

if guess3== code[0]:
    wronPlace= (wronPlace+1)
if guess3== code[1]:
    wronPlace= (wronPlace+1)
if guess3== code[3]:
    wronPlace= (wronPlace+1)

if guess4== code[0]:
    wronPlace= (wronPlace+1)
if guess4== code[1]:
    wronPlace= (wronPlace+1)
if guess4= code[2]:
    wronPlace= (wronPlace+1)


What I have tried:

I've tried to turn it into a sub-routine or shorten it manually but have had no luck. Could anyone help?
Posted
Updated 11-Feb-18 3:56am
v2

No shorter, but may be debugged:
Python
if guess4== code[0]:
    wronPlace= (wronPlace+1)
if guess3== code[1]:
    wronPlace= (wronPlace+1)
if guess3== code[2]:
    wronPlace= (wronPlace+1)

Are you sure about the guess3 in this part?

[Update]
Quote:
Guess1 Guess2 Guess3 and Guess4 are the four digits I split apart from guess. They need to be compared to the four generated digits

Read carefully your code!
you check guess1 3 times,
then you check guess2 3 times,
then you check guess3 3 times,
at this point, one can expect you to check guess4 3 times but,
you check guess4 1 time and guess3 2 times.
Is it what you want?

[Update]
Quote:
Codebreaker: It is a game where you guess 4 digits of a code. It outputs the amount of correct digits in the correct place and amount of correct digits in the WRONG place. The code is stored as a array. You have 12 guesses to guess the code

OK. So your code is plain wrong because you don't handle when there is 2 identical digits in code or guesses.
Example: code is 1123 and guess is 3211, your program will say there is 6 wrong placed digits.
You have to rethink the logic.
 
Share this answer
 
v4
Comments
Member 13672432 11-Feb-18 8:58am    
Guess1 Guess2 Guess3 and Guess4 are the four digits I split apart from guess. They need to be compared to the four generated digits
Patrice T 11-Feb-18 9:47am    
see update of solution.
Member 13672432 11-Feb-18 9:55am    
Thanks I missed that.
Patrice T 11-Feb-18 13:11pm    
by the way, can you explain what is supposed to do the whole program?
Member 13672432 11-Feb-18 13:49pm    
Codebreaker: It is a game where you guess 4 digits of a code. It outputs the amount of correct digits in the correct place and amount of correct digits in the WRONG place. The code is stored as a array. You have 12 guesses to guess the code
 
Share this answer
 
Comments
Member 13672432 11-Feb-18 8:58am    
An array has already been used
Richard MacCutchan 11-Feb-18 12:39pm    
Then why is it not working?
Member 13672432 11-Feb-18 17:26pm    
Have you read the question and snippet of code properly? https://www.codeproject.com/Questions/1229708/How-can-I-change-this-code-to-give-the-desired-res
Richard MacCutchan 12-Feb-18 4:06am    
Why have you reposted the question?
Member 13672432 16-Feb-18 17:56pm    
My question is different and due to you It has not been answered and it never will be

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900