I have tried to validate some input for my homework. Requirements:
AMEX card
VISA card
if card number with 15 digits but does not start with 34, 37, print:
You have entered an invalid Amex card numbers
if card number with 16 digits but does not start with 4, print: You have entered an invalid Visa card numbers
You have entered an invalid credit card number
Amex
or Visa
) and the last 4 digits of the credit card number.I have tried to do len(number)
to find the number of digits from user input. However, I keep getting the error "invalid syntax"
number = int(input("Enter your credit card number :"))
if len(number) = 16 and number.startswith(4):
validity = "valid"
if len(number) = 15 and number.startswith(34) and number.startswith(37):
validity = "valid"
else:
validity = "invalid"
I cannot seem to get the correct outcome that I want.
This is how the program should run:
Enter your credit card number : 432143214321432
You have entered an invalid Amex card numbers
Enter your credit card number : 3456345634563456
You have entered an invalid Visa card numbers
Enter your credit card number : 123
You have entered an invalid credit card number
Enter your credit card number : 4321432143214321
You have a valid Visa card and the last 4 digit is 4321
if len(number) = 16 and number.startswith(4):
len(number)
. Number does not have a len, so either convert number to a string here or don't convert it to an int in the first placenumber.startswith(4):
. Same for this, ints do not have a startswith() method==
is the comparison operator, not =
number = input("Enter your credit card number :")
if len(number) == 16 and number.startswith('4'):
validity = "valid"
if len(number) == 15 and number.startswith('34') or number.startswith('37'):
validity = "valid"
else:
validity = "invalid"
If you need number as an int (unlikely for a credit card number) convert it with
number = int(number)