Я работаю над небольшим сценарием для проверки кредитных карт и срока действия банка, но по какой-то сумасшедшей причине я получаю сообщение об ошибке, которое я не понимаю.Код python для проверки кредитной карты и банка
#!/usr/bin/python
# -*- coding: utf-8 -*-
def digitSum(myString):
length = len(myString)
oddSum = 0
evenSum = 0
#base case
if (length ==0):
return 0
#length > 0
else:
#if even
if (length % 2 ==0):
last = int(myString[-1])
evenSum += last
return evenSum + digitSum(myString[:-1])
else:
last = int(myString[-1])
last = 2 * last
part_sum = last // 10 + last % 10
oddSum += part_sum
return oddSum + digitSum(myString[:-1])
def luhns():
myString = input("Entrez les 16 numéros de la Carte de Crédit ")
total = digitSum(myString)
if (total % 10 == 0):
if total[:1] == "4":
cardtype = "Visa"
if int(total[:2]) >= 51 and int(total[:2]) <= 55:
cardtype = "Master Card"
if total[:2] == "34" or total[:2] == "37":
cardtype = "American Express"
if total[:4] == "6011":
cardtype = "Discover"
if total[:2] == "36":
cardtype = "DINERS"
if int(total[:3]) >= 300 and int(total[:3]) <= 305:
cardtype = "DINERS"
return cardtype
print ('Carte valide') + cardtype
else:
print ('Carte invalide')
def main():
luhns()
#Python call to main()
main()
Это ошибка, я получаю:
Entrez les 16 numéros de la Carte de Crédit 11111
Traceback (most recent call last):
File "/Volumes/Python/LuhnsAlgorithm.py", line 61, in <module>
main()
File "/Volumes/Python/LuhnsAlgorithm.py", line 58, in main
luhns()
File "/Volumes/Python/LuhnsAlgorithm.py", line 34, in luhns
total = digitSum(myString)
File "/Volumes/Python/LuhnsAlgorithm.py", line 5, in digitSum
length = len(myString)
TypeError: object of type 'int' has no len()
logout
'myString' представляет собой целое число. –
Не должно быть 'raw_input'? – melpomene
В зависимости от того, используете ли вы Python 2 или 3, 'input' ведет себя по-другому .. Python 2' input' будет оценивать входную строку и преобразовывать ее в целое число, если это целое число. Вы должны использовать Python 2. Если вы не хотите, чтобы он оценивался, вам нужно использовать 'raw_input'. См. Ответы на [Различия между 'input' и' raw_input'] (http://stackoverflow.com/questions/3800846/differences-between-input-and-raw-input) –