If statements in functions

I am relatively new to python but am currently stuck on this program. My goal is too validate a credit card number by testing the length and the first number
I have figured out the length requirement but as you can see in the code below I use the == sign to check if the first digit is equal to either 4, 5 ,6 or 38 but whatever number I put in it comes out as valid? Keep in mind I am using Pycharm
Any help or recommendations would be appreciated
(input)
“”“‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
This function will validate a credit card number
“””
#This function checks the length of the card number

def main():

number = str(input("Enter your credit card number (must be between 13 and 16 digits): "))

# This function checks the length of the card number
def isValid(number):
    max = 16
    min = 13
# check if the card number length is to long
    if len(number) > max:
        print("The number length is Invaid")

# check if the card number length is too short
    elif len(number) < min:
        print("The number length is Invaid")

# if length is correct check if it has correct first digit
    else:
        print("The number length is Valid")
        print("Number is", number)
        number_str = str(number)
        first_digit = number_str[0]

        if first_digit == 4 or 5 or 6:
            print("The First Card number is Valid")

        elif number_str[0] == 3 and number_str[1] == 8:
            print("The First Card number is Valid")

        else:
            print("First Card Number not Valid")
            quit()
isValid(number)

main()
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
(output)
Enter your credit card number (must be between 13 and 16 digits): 20000000000000
The number length is Valid
Number is 20000000000000
The First Card number is Valid

This line is interpreted as if (first_digit == 4) or (5) or (6), which is always true because 5 is always true. That is, the first_digit == 4 condition fails, so it moves on to the 5 condition, which succeeds.

Instead, try if first_digit == 4 or first_digit == 5 or first_digit == 6 or if first_digit in (4, 5, 6)[1].


  1. You’ll run into another bug as soon as you do that, but see if you can figure it out :slight_smile: ↩︎

The input function returns a string, so it’s not necessary to use str(...) on it.

You don’t need number_str = str(number) because number is already a string.

@Zachary has already mentioned that first_digit == 4 or 5 or 6 is equivalent to (first_digit == 4) or (5) or (6), which is always true because 5 is treated as true, but as number_str is a string, number_str is also a string. 4, 5 and 6 are numbers, and strings are never equal to numbers, e.g. “4” != 4.

Get rid of first_digit and number_str. Do instead:

if number[0] in ("4", "5", "6"):   # alternatively:  if number[0] in "456":

And instead of

if number_str[0] == 3 and number_str[1] == 8:

do:

if number[:2] == "38":   # alternative: if number.startswith("38"):
    ...

If I had to grade your program, you could get bonus points if you implemented the Luhn algorithm: Luhn algorithm - Wikipedia :slight_smile: