-3

I need to write a python code which checks for equality for the entered pin second time and then proceeds further. Here is how my code goes:

id1 = int(input("Enter 4-digit account pin: "))
id2 = int(input("Re-Enter 4-digit account pin for confirmation: "))
if id1 == id2:
    id = id1
else:
    id2 = int(input("Incorrect pin.. Please Re-enter: "))

Here after my pin gets verified it needs to get assigned to a variable named id. But the above code works only when I enter the incorrect pin once. If I enter again, the code continues to the next step. I want the code to repeat the equality check until both the entered pins are equal and after confirmed as equal, needed to get assigned to the variable id. Please help.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Mujeebur Rahman
  • 189
  • 1
  • 14

2 Answers2

2

Use a while loop:

id1 = int(input("Enter 4-digit account pin: "))
id2 = int(input("Re-Enter 4-digit account pin for confirmation: "))
while id1 != id2:
    id2 = int(input("Incorrect pin.. Please Re-enter: "))
id = id1

This repeatedly asks for PIN confirmation until the two values are equal.

EnderShadow8
  • 788
  • 6
  • 17
1

Wrap the whole thing in a while id1 != id2 loop.

# initialize to different values so the input loop will run at least once
id1 = 1
id2 = 2

# keep asking for both inputs until they are equal
while id1 != id2:
    id1 = int(input(...))
    id2 = int(input(...))

    if id1 != id2:
        print("Incorrect.  Please re-enter")

# loop is done, so id1 is the same as id2
id = id1
John Gordon
  • 29,573
  • 7
  • 33
  • 58