0

I have this little credit card validation program where the user has to input an 8 digit integer. The program works fine, my only problem seems to be limiting user input to 8 digits only.

prompt = "Please, enter the 8 digit number of your card: "

while True: 
    try:
        userinput = int(raw_input(prompt))
    except ValueError: 
        print('The gematric value must be an 8 digit integer. Try again')
    else:
        break 

userinput = str(userinput)

a = int(userinput[7])+int(userinput[5])+int(userinput[3])+int(userinput[1])
b1 = str(int(userinput[6])*20)
b2 = str(int(userinput[4])*20)
b3 = str(int(userinput[2])*20)
b4 = str(int(userinput[0])*20)
y = int(b1[0])+int(b1[1])+int(b2[0])+int(b2[1])+int(b3[0])+int(b3[1])+int(b4[0])+int(b4[1])

x = (a+y)

if x % 10 == 0:
   print('The card number you entered is valid!')
else:
   print('The card number you entered is invalid!')

I know len() does not work with integers. So how would i incorporate that 8 digit limit into my while true loop?

Any help would be appreciated.

0

1 Answer 1

3

Try the following.

try:
    userinput = raw_input(prompt)
    if len(userinput ) > 8:
        raise ValueError()
    userinput = int(userinput)
except ValueError:
...
Sign up to request clarification or add additional context in comments.

1 Comment

Great, i just changed > to != and it works perfect. Thank you.

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.