“字符串索引超出范围行7”。怎么了?

时间:2017-02-19 16:24:21

标签: python string for-loop range cs50

任务: 报告用户输入的信用卡号是否为:

VISA,MASTERCARD,AMEX或INVALID

Amex:15位数 - 从34/37开始

万事达卡:16位 - 从51/52/53/54/55开始

签证:13-16位 - 从4开始

https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm

要查看的快速号码: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()

2 个答案:

答案 0 :(得分:0)

首先i将为零。 所以x[digits-0]肯定会超出范围。

让我们将循环更改为

for i in range (1,digits+1):

此外,您应该在使用str之前将输入转换为len()类型。

修改后的代码

#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 (1,digits+1):
        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 (1,digits+1):
        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 = str(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()

答案 1 :(得分:0)

这是一个经典的 off-by-one 错误

在结果中添加一个

digit = int(x[digits-i])

喜欢这个

digit = int(x[digits-i+1])