-1

I am trying to make a credit card checker which displays whether the input aka the card number is invalid, Mastercard, visa, or Amex.

i want the code to re ask the user to enter a number if a user enters any letters, special characters, spaces, or nothing. But in the code for Luhn's algorithm i have to change the "inp" to a int to multiply but then the code ends with an error then if the input contains any invalid input. How can I make it prompt again if the input is invalid?

while True:

    inp = (input("Enter a number!: "))

    inp_1 = inp[0::]
    inp_2 = inp[0::]
    inp_3 = inp[0::]
    inp_4 = inp[0::]

    reversed_inp = inp[::-1]

    inp = reversed_inp[1::2]


    value = 0
    inp = str(inp)
    for character in inp:
        character = int(character)
        character *= 2
        character = str(character)
        character = int(character)
        for i in range(len(str(character))):
            digit = character%10
            value = value + digit
            character = character//10

    reversed_inp_2 = inp_1[::-1]

    inp_1 = reversed_inp_2[0::2]

    value_1 = 0
    
    for numbers in inp_1:
        numbers = int(numbers)
        
        
        value_1 = value_1 + numbers

    final_value = value_1 + value

    final_value = str(final_value)
    reversed_final_value = final_value[::-1]

    zero_checker = reversed_final_value[0:1:]
    zero_checker = (zero_checker)


    if zero_checker == 1 or  zero_checker == 2 or zero_checker == 3 or zero_checker == 4 or zero_checker == 5 or zero_checker == 6 or zero_checker == 7 or zero_checker == 8 or zero_checker == 9:
        print("INVALID")
        break
   
    def countDigits(inp_3):
        dcount = 0
        for c in inp_3:
            if c.isdigit():
                dcount = dcount + 1
        return dcount
    
    digits = countDigits(inp_3)
    digits = str(digits)


    Amex_checker = inp_2[0:2:]
    Amex_checker = str(Amex_checker)
    MasterCard_checker = inp_2[0:2:]
    MasterCard_checker = str(MasterCard_checker)
    Visa_Checker = inp_2[0:1:]
    Visa_Checker = str(Visa_Checker)

    if Amex_checker == "34" and digits == "15":
        print("AMEX")
        break
    elif Amex_checker == "37" and digits == "15":
        print("AMEX")
        break

    if MasterCard_checker == "51" and digits == "16":
        print("MASTERCARD")
        break
    elif MasterCard_checker == "52" and digits == "16":
        print("MASTERCARD")
        break
    elif MasterCard_checker == "53" and digits == "16":
        print("MASTERCARD")
        break
    elif MasterCard_checker == "54" and digits == "16":
        print("MASTERCARD")
        break
    elif MasterCard_checker == "55" and digits == "16":
        print("MASTERCARD")
        break

    if Visa_Checker == "4" and digits == "16" or digits == "13":
        print("VISA")
        break
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Mussa Zeb
  • 1
  • 1
  • Welcome to Stack Overflow. "i added the while true loop its just that i am new to stack overflow and it is not showing in the code for some reason but everything is in the while loop." Please read [ask] and especially the [formatting help](https://stackoverflow.com/help/formatting). Note well that this is **not a discussion forum**. I [edit]ed the post to show how to ask a bit more directly and clearly, but the question is still a mess. When posting here, please try to come up with sample code that *directly demonstrates the problem*; don't just show us the entire original code. See [mre]. – Karl Knechtel Oct 05 '22 at 01:34

1 Answers1

0

This asks the user to enter a number until it is a valid integer:

user_in = None
while user_in is None:
    try:
        user_in = int(input("Give me a number:"))
    except ValueError:
        print("Invalid input.")

You could edit one line, to get a valid string, instead of an int:

user_in = str(int(input("Give me a number:")))
H.Syd
  • 106
  • 3