我的代码是错误的,我不知道该怎么办

时间:2019-05-21 14:51:08

标签: python

我认为问题出在def sumOfOddPlace和def sumOfDoubleEvenPlace。

我不知道该怎么做。

def checkSize(creditCardNumber):
    length = len(creditCardNumber)
    creditCardNumber = len(creditCardNumber)
    if creditCardNumber == 16:
        return True
    else:
        return False

def checkPrefix(creditCardNumber):
    for i, c in enumerate(creditCardNumber):
        if c.isdigit():
            break

        if i == 4:
            return True
        else:
            return False

def sumOfOddPlace(creditCardNumber):
    length = len(creditCardNumber)
    sumOdd = 0

    for i in range(length-1,-1,-2):
        creditCardNumber += eval(creditCardNumber[i])
    return sumOdd

def sumOfDoubleEvenPlace(creditCardNumber):
    length = len(creditCardNumber)
    sum =  0
    for i in range(length-2,-1,-2):
        number = eval(creditCardNumber[i])
        number = number * 2
        if number > 9:
           strNumber = str(number)
           number = eval(strNumber[0]) + eval(strNumber[1])
           sum += number
        return sum

def main ():

    infile = open ("input.dat", "r")
    outfile = open ("output.dat", "w")

    creditCardNumber = infile.readline()

    while creditCardNumber != '':
        outfile.write ("Credit card number entered: ")
        outfile.write (creditCardNumber)

        if((not(checkSize(creditCardNumber))) or (not(checkPrefix(creditCardNumber))) or (((sumOfDoubleEvenPlace(creditCardNumber) + sumOfOddPlace(creditCardNumber)) % 10) != 0)):
        outfile.write ("Credit card number is invalid.\n")

     else:
         outfile.write ("Credit card number is valid.\n")

     creditCardNumber = infile.readline()

 infile.close()
 outfile.close()

main()

我的输出不是应该的。

输出应为:

Credit card number entered: 4388576018
Credit card number is invalid.
Credit card number entered: 43885760184026263432423423
Credit card number is invalid.
Credit card number entered: 2388576018410707
Credit card number is invalid.
Credit card number entered: 4388576018410707
Credit card number is valid.

但这是

Credit card number entered: 4388576018
Credit card number is invalid.
Credit card number entered: 43885760184026263432423423
Credit card number is invalid.
Credit card number entered: 2388576018410707
Credit card number is invalid.
Credit card number entered: 4388576018410707Credit card number is invalid.

抱歉,这是我第一次使用此网站,代码混乱。

0 个答案:

没有答案