Aufgabe: Berichte, ob der Benutzer eingegebene Kreditkartennummer ist:"String Index außerhalb des Bereichs Linie 7." Was ist falsch?
VISA, Mastercard, AMEX oder INVALID
Amex: 15 Stellen - beginnt mit 34/37
Mastercard: 16 Digits - - beginnt mit 51/52/53/54/55
Visa: 13-16 Digits - beginnt mit 4
https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
Schnellnummer überprüfen mit: 378282246310005 "AMEX"
#Luhn algorithm to check credit card's number validity
def Luhn (x, digits):
#Sum of every other number starting from before last n
sum1 = 0
i = 2
for i in range (digits):
digit = int(x[digits-i])
if 2 * digit < 10: sum1 = sum1 + digit *2
else: sum1 = sum1 + 1 + (2 * digit -10)
i+=2
#Some of every other number starting from last n
sum2 = 0
i = 1
for i in range (digits):
digit = int(x[digits-i])
sum2 = sum2 + digit
i+=2
#Some of both and condition return
if (sum1 + sum2) % 10 == 0: return True
else: return False
def main():
x = input("Insert your credit card's number for analysis\n")
# Length check
digits = len(x)
if digits <13 >16:
print("Invalid Length")
return 1
# Luhn algorithm check
check = Luhn(x, digits)
if check == False:
print("Invalid Card Number 'luhn'")
return 1
# Assigning letter id to first to numbers to determine card type
creditid1 = int(x[0])
creditid2 = int(x[1])
# Checking digits count and first to number to determine card type
if digits == 15 and creditid1 == 3 and (creditid2 == 4 or 7):
print("AMEX\n")
elif digits == 16 and creditid1 == 5 and (creditid2 == (1,6)):
print("MASTERCARD\n")
elif digits == 13 or 16 and creditid1 == 4:
print("VISA\n")
else:
print("Not Amex...\n Not Mastercard... \n Not Visa... \n")
if __name__ == "__main__":
main()
Zuerst werde ich Null sein. x [Ziffern] liegt außerhalb des Bereichs – jophab
Ja, aber das Programm verwendet niemals x [Ziffern]. Es beginnt mindestens mit x [Ziffern-1]. Aber warum wird es sowieso außer Reichweite sein? – user627154
Bereich (n) erzeugt Werte von 0 bis n-1. –