checks validity of credit card in python (cs50 pset6)
Your program would benefit from breaking up logic into functions. This also eliminates the global variables involved.
if i%2 == (-2+len(number))%2

This line is strange and not immediately obvious what it's doing. A reversed iteration would be easier to understand.
import re

def luhn_digit(digit,double):
    if double:
        doubled = digit*2
        return doubled-9 if doubled&gt;9 else doubled
    else:
        return digit

def is_valid_luhn(card):
    check_digit = int(card[-1])
    luhn_sum = sum([
        luhn_digit(int(digit),i%2==0)
        for i,digit in enumerate(reversed(card[:-1]))
    ])
    total = check_digit+luhn_sum
    return total%10==0

def is_valid_card(card):
    return (
        len(card) in (13,15,16) and
        is_valid_luhn(card)
    )

def get_card_type(card):
    if not is_valid_card(card):
        return &quot;INVALID&quot;
    if re.search(&quot;^3(4|7)&quot;, card):
        return &quot;AMEX&quot;
    if card[0] == str(4):
        return &quot;VISA&quot;
    if re.search(&quot;^5(1|2|3|4|5)&quot;, card):
        return &quot;MASTERCARD&quot;
    return &quot;INVALID&quot;

print(get_card_type(input(&quot;NUMBER: &quot;)))