0

I'm writing a python program that first generates a three-digit code for the lock. The program asks the user for the numbers of the guess one by one and checks after each entered number that the user has not already entered it. The numbers of the guess should be added to the list and checked before each addition if the number is already in the list. If the guess is correct, the program prints "Correct! You cracked the code!" and terminates. If the guess does not contain at least one of the numbers in the lock code, the program prints "Nothing is correct."If the guess has at least one number the same as the lock code, the program prints how many numbers are correct and correctly placed and how many numbers are correct but incorrectly placed. Furthermore, the number of guesses should not exceed 5 times, otherwise it terminates and prints "Time is over".

Expected output:
Can you crack the code of a three-digit lock?
Enter a seed:
2
Enter 1. number of your guess.
0
Enter 2. number of your guess.
1
Enter 3. number of your guess.
2
2 numbers were correct and correctly placed.
0 numbers were correct, but incorrectly placed
Enter 1. number of your guess.
0
Enter 2. number of your guess.
1
Enter 3. number of your guess.
4
2 numbers were correct and correctly placed.
0 numbers were correct, but incorrectly placed
Enter 1. number of your guess.
0
Enter 2. number of your guess.
1
Enter 3. number of your guess.
8
Correct! You cracked the code!
My code:
import random

CODE_NUMBERS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

def main():
  print("Can you crack the code of a three-digit lock?")
  # Generate the random three-digit code
  seed_number = int(input("Enter a seed:\n"))
  random.seed(seed_number)
  right_code = random.sample(CODE_NUMBERS, 3) # the lock code

  # Implement your code here
  guess = 1
  code = []
  number1 = int(input('Enter 1. number of your guess.\n'))
  number2 = int(input('Enter 2. number of your guess.\n'))
  while number1 == number2:
    print('The key cannot have two same numbers.')
    number2 = int(input('Enter 2. number of your guess.\n'))
  number3 = int(input('Enter 3. number of your guess.\n'))
  while number3 == number1 or number3 == number2:
    print('The key cannot have two same numbers.')
    number3 = int(input('Enter 3. number of your guess.\n'))
    
  code.append(number1)
  code.append(number2)
  code.append(number3)

  while guess <= 5:
    count1 = 0
    count2 = 0

    if code[0] != right_code[0] and code[1] != right_code[1] and code[2] != right_code[2]:
      if code[0] and code[1] and code[2] not in right_code:
        print('Nothing is correct.')
      else:
        for i in range(len(code)):
          if code[i] == right_code[i]:
            count1 += 1
          elif code[i] in right_code and code[i] != right_code[i]:
            count2 += 1
        print(f"{count1:d} numbers were correct and correctly placed.")
        print(f"{count2:d} numbers were correct, but incorrectly placed")

      code.clear()
      guess += 1
      number1 = int(input('Enter 1. number of your guess.\n'))
      number2 = int(input('Enter 2. number of your guess.\n'))
      while number1 == number2:
        print('The key cannot have two same numbers.')
        number2 = int(input('Enter 2. number of your guess.\n'))
      number3 = int(input('Enter 3. number of your guess.\n'))
      while number3 == number1 or number3 == number2:
        print('The key cannot have two same numbers.')
        number3 = int(input('Enter 3. number of your guess.\n'))

      code.append(number1)
      code.append(number2)
      code.append(number3)
      
    elif code[0] == right_code[0] and code[1] == right_code[1] and code[2] == right_code[2]:
      print('Correct! You cracked the code!')
  
  print('Time is over! The correct code is ', right_code)

main()

It prints "Correct..." several times while I expect there should be at once.

1
  • You'll find it easier if user inputs a 3-digit number rather than 3 separate values. Validation will be more straightforward. I wouldn't bother with the seed Commented Sep 23, 2022 at 9:19

1 Answer 1

1

The problem is the following line:

while guess <= 5:

You start of with guess = 1. Then later on you have:

if code[0] != right_code[0] and code[1] != right_code[1] and code[2] != right_code[2]:
 ...
 guess += 1

But after this line:

elif code[0] == right_code[0] and code[1] == right_code[1] and code[2] == right_code[2]:

you don't change the guess value. And after that it goes back to the while and checks the guess, that still isn't 5 because you didn't change it. So you can either:

  • Add a line to the elif saying guesses == 5

  • Change the condition for the while loop and make it and

I would do the second one. So the while loop keeps going until you either have 5 wrong guesses, or the right answer.

By the way, you can change

code[0] == right_code[0] and code[1] == right_code[1] and code[2] == right_code[2]

to

code == right_code

That makes it a little better to read

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, I changed this but still does not work: elif code == right_code: print('Correct! You cracked the code!') guess = 5
I edited the code with the changed, lines. You can find it on this link.
Something you can do to make it easier, what is also seen as a better way, is to put the code guessing you do multiple times into a function. Like this. It's the same code, but just written shorter as a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.