0

I'm very new at Python, and I'm trying to make a simple program that asks the user for a password, and if the user didn't guess the password they were asked, ask again until they guess it. How do I do that?

Password = input("guess the password: ")
while (password) != "12345":
    Print(input("try again : "))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Breadoggo
  • 1
  • 2

3 Answers3

1

Welcome to Programming and StackOverflow. Have a look a this Example,

n = 5
while n > 0:
    n -= 1
    if n == 2:
        break
    print(n)
print('Loop ended.')

The break statement ends the while loop.

@g23's Answer is more apt in the context of the question

Retic
  • 199
  • 2
  • 12
0

Make sure your capitalization is right, generally variables are lowercase, but you need to be consistent.

Also when you ask for the password again, you need to store what the user gives you so it can be checked in the loop condition (the while password != "12345": part)

Something like

password = input("Enter the password: ")
while password != "12345":
    password = input("try again: ")
g23
  • 666
  • 3
  • 9
0

This code does what you want. It has a while loop to check if the password was guessed until the right password is entered. Then it has an if statement to write a message: if the right password is entered, it writes it.

password = input("Enter the password: ")
while password != "12345":
    password = input("try again: ")
    if password == "12345":
        print("Correct password!")
hellomynameisA
  • 546
  • 1
  • 7
  • 28