0

I am doing this for a school project, but I haven't been able to figure out how to do this last part and get it to work properly. This is quite simple so I would appreciate any quick help.

I am coding a system that randomly generates a three-digit code, and the user has to guess what it is. They have unlimited tries, and I figured out how to get it to loop, but it won't stop looping when they guess the code right. How do I do this?

Current Code (With Error):

import random
NUMBERS1 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS2 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS3 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
code = random.choice(NUMBERS1) + random.choice(NUMBERS2) + random.choice(NUMBERS3)
print(code)
while True:
  codeguess = input('Guess the 3 Digit Code: ')
  if codeguess == (code):
    print('Good Job! The Code was ' + code)
  else:
    print('Wrong! Try Again!')
while False:
  print('Eyyyyy')
1
  • 2
    Use break to get out of a loop. Commented Jan 21, 2021 at 18:50

4 Answers 4

1

You need to add a break statement in the if condition in order to break the loop when the user enters correct code, just like this:

import random
NUMBERS1 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS2 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS3 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
code = random.choice(NUMBERS1) + random.choice(NUMBERS2) + random.choice(NUMBERS3)
print(code)
while True:
  codeguess = input('Guess the 3 Digit Code: ')
  if codeguess == (code):
    print('Good Job! The Code was ' + code)
    break
  else:
    print('Wrong! Try Again!')

And you don't need the while False: loop. That is a separate infinite loop and will start only after the first while loop ends.

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

Comments

1

Use break statement when the correct code is guessed.

while True:
  codeguess = input('Guess the 3 Digit Code: ')
  if codeguess == (code):
    print('Good Job! The Code was ' + code)
    break
  else:
    print('Wrong! Try Again!')

Comments

1

use:

import random
NUMBERS1 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS2 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS3 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
code = random.choice(NUMBERS1) + random.choice(NUMBERS2) + random.choice(NUMBERS3)
print(code)
while True:
  codeguess = input('Guess the 3 Digit Code: ')
  if codeguess == (code):
    print('Good Job! The Code was ' + code)
    break
  else:
    print('Wrong! Try Again!')

Comments

0

Once the codeguess is equal to code, inside that if statement you need to assign a variable to cause the while loop to exit. The second while loop is not needed. I have put my solution below:

import random
NUMBERS1 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS2 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
NUMBERS3 = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
code = random.choice(NUMBERS1) + random.choice(NUMBERS2) + random.choice(NUMBERS3)
print(code)
# add this line
guessright = False
# change this while condition
while not guessright:
  codeguess = input('Guess the 3 Digit Code: ')
  if codeguess == (code):
    print('Good Job! The Code was ' + code)
    #add this to exit loop
    guessright = True
  else:
    print('Wrong! Try Again!')
# remove this code
# while False:
#   print('Eyyyyy')

Comments

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.