0

This is the first time im writing a program with python and im trying to only allow the user to input 8 digits. I managed to do this and every time i enter more or less than 8 it gives the error message which is good but after that if i enter 8 digits it still gives the error message

    value3 = input("please enter your card number: ")
while not value3.isdigit():
    value3= input("please enter your card NUMBER: ")
if len(value3) > 8:
    while True:
        value3 = input("Error! Only 8 characters allowed!: ")
if len(value3) < 8:
    while True:
        value3 = input("Error! Only 8 characters allowed!: ")
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50

2 Answers2

2

You should not use While True: in your if statements.

You can do something like this:

value3 = input("please enter your card number: ")

while True:
    if not value3.isdigit():
        value3= input("please enter your card NUMBER, Only digits are allowed: ")
        continue
    elif len(value3) != 8:
        value3 = input("Error! Only 8 digits allowed!: ")
    else:
        print("Valid card number")
        break
Megabeets
  • 1,378
  • 11
  • 19
1

Well it appears that you have the "error messages" in while loops with the condition always set to true. So no matter what you enter, your program is still stuck in that while loop. A better approach might be to define an "error function" that gets called when the input is more/less than 8.

SuperStew
  • 2,857
  • 2
  • 15
  • 27