简体   繁体   中英

deal with while loop and if-else statement in python

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM