Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am making a game called Codebreaker. A 4 digit number is generated while the user guesses the number. It should output the amount of correct digits (up to 4) in the correct place and amount of correct digits in the WRONG place (up to 4). There are 12 lives and this is what I have done so far

import random

code=[random.randint(1,9), random.randint(0,9), random.randint(0,9), random.randint(0,9)]
code=[2, 3, 6, 3]
display_answer= ["X","X","X","X"]
guesses= 1
corrPlace= 0
wronPlace= 0

instructions= input("Welcome to Codebreaker! Would you like to see the instructions type 'Yes' or 'No' ")
if (instructions == "Yes") or (instructions == "yes"):
  print("I will randomly generate a 4 digit number, try and guess it using your keyboard.")
else:
  print("Alrighty")


mode= input("What mode would you like to play on? 'Easy', 'Medium' or 'Hard': ")
mode= mode.lower()
if mode== "easy":
  print("Let's get on with it!" )

  print('\n')
  print("I am thinking of a four digit number, try and guess that number!")
  
  while True:
    print("You have: " , 13 - guesses, " guesses left")
    guess = input("Have a guess! Type 'quit' to quit: ")
  
    if guess == "quit":
      print('\n')
      print("You quit! The number I was thinking of was: ")
      print(code)
      print('\n')
      print("xX !YOU LOSE! Xx")
      break
  
    elif len(guess) != 4:
      print("The code is 4 digits long! Go again.")
      print('\n')
      continue

    elif guess.isdigit()== False:
      print("Please type in a NUMBER guess!")
      print('\n')
      continue

    if guesses == 12:
      print("You have no guesses left...")
      print("The number I was thinking of was: ", code)
      print("xX !YOU LOSE! Xx")
      break
    guesses = guesses+1

    guess1= int(guess[0])
    guess2= int(guess[1])
    guess3= int(guess[2])
    guess4= int(guess[3])

    if guess1== code[0]:
      display_answer[0]= guess1
    if guess2== code[1]:
      display_answer[1]= guess2
    if guess3== code[2]:
      display_answer[2]= guess3
    if guess4== code[3]:
      display_answer[3]= guess4
    
    print("You have guessed this part correctly:")
    print(display_answer, '\n')

    if display_answer[0] != "X" and display_answer[1] != "X" and display_answer[2] != "X" and display_answer[3] != "X":
      print('\n')
      print("You won! The number I was thinking of was: ")
      print(code)
      print('\n')
      print("xX !YOU WON! Xx")
      break

elif mode== "medium":
    ###
    print('\n')
    print("I am thinking of a four digit number, try and guess that number!")
    print (code)
    
    while True:
      print("You have: " , 13 - guesses, " guesses left")
      guess = input("Have a guess! Type 'quit' to quit: ")
    ###

    ###
      if guess == "quit":
          print('\n')
          print("You quit! The number I was thinking of was: ")
          print(code)
          print('\n')
          print("xX !YOU LOSE! Xx")
          break
    ###

    ###
      elif len(guess) != 4:
        print("The code is 4 digits long! Go again.")
        print('\n')
        continue
    ###

    ###
      elif guess.isdigit()== False:
        print("Please type in a NUMBER guess!")
        print('\n')
        continue
    ###

    ###
      if guesses == 12:
        print("You have no guesses left...")
        print("The number I was thinking of was: ", code)
        print("xX !YOU LOSE! Xx")
        break
      guesses = guesses+1
    ###

    ###
      guess1= int(guess[0])
      guess2= int(guess[1])
      guess3= int(guess[2])
      guess4= int(guess[3])
    ###

    ####
      corrPlace= 0
      if guess1==code[0]:
          corrPlace= (corrPlace+1)
      if guess2==code[1]:
          corrPlace= (corrPlace+1)
      if guess3== code[2]:
          corrPlace= (corrPlace+1)
      if guess4== code[3]:    
          corrPlace= (corrPlace+1)
    ####

    #####
      wronPlace= 0
      if guess1=! code[0]:
        if guess1== code[1] or code[2] or code[3]:
          wronPlace= (wronPlace+1)
        
      if guess2=! code[1]:
        if guess2== code[0] or code[2] or code[3]:
          wronPlace= (wronPlace+1)

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

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

      if corrPlace== 4:
        wronPlace= 0

    #####

    ###
      print ("You have guessed ", corrPlace, " digits in the correct place.")
      print ("But there are ", wronPlace, " correct digits in the wrong place.")
      print ('\n')

      if corrPlace== 4:
          print('\n')
          print("You won! The number I was thinking of was: ")
          print(code)
          print('\n')
          print("xX !YOU WON! Xx")
          break
    ###

elif mode== "hard":
  print ("Under construction")


The problem is with this:
#####
  wronPlace= 0
  if guess1=! code[0]:
    if guess1== code[1] or code[2] or code[3]:
      wronPlace= (wronPlace+1)

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

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

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

  if corrPlace== 4:
    wronPlace= 0


It should output up to four digits but it mimmicks the corrPlace variable. The plan I've done for this piece of code is this:
Quote:
compare code to guess same number
1 to [0]
2 to [1]
3 to [2]
4 to [3]

NOT switch

if same then 1
if different then 0

1=0
0=1

if [1] matched dont compare (FALSE)
if [0] matched dont compare
if [2] matched dont compare
if [3] matched dont compare

if [1] not matched compare (TRUE)
if [0] not matched compare
if [2] not matched compare
if [3] not matched compare


if compared to [1] gives TRUE stop
if false carry on
if compared to [0] gives TRUE stop
if false carry on
if compared to [2] gives TRUE stop
if false carry on
if compared to [3] gives TRUE stop
if false carry on

TRUE= wronPlace+1
4 = max

Please help me change this piece of code into the plan below! I've struggles a while with this

What I have tried:

Before the plan it ried using a while loop, if statemts and everything that led to failure. I managed to come up with the basic plan though using my knowledge of the Boolean field and assessing the situation using 1 and 0 (True and False). I predict the continue keyword should be present
Posted
Updated 12-Feb-18 16:26pm
Comments
Richard Deeming 13-Feb-18 11:43am    
REPOST
You have already posted this homework:
https://www.codeproject.com/Questions/1229655/How-may-I-shorten-this-code-Python[^]

If you want to update your question to add missing information, click the green "Improve question" link and edit your question. DO NOT post the update as a new question.
Member 13672432 16-Feb-18 18:14pm    
Not homework. I'm trying to teach myself coding and you're not helping.

1 solution

Your first problem is that
Python
if guess1== code[1] or code[2] or code[3]:

is not python.
correction is
Python
if guess1== code[1] or  guess1== code[2] or  guess1== code[3]:


Your problem is that an element of code can match a guess more than once. You have to device an algorithm that prevent it to from happening.
 
Share this answer
 
Comments
Member 13672432 16-Feb-18 18:16pm    
I've devised a strategy to do so but it takes too many ifs and elifs to keep track of which causes it to be confusing

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