0

I'm working on a small script to check credit cards and bank validity but for a crazy reason I get an error I do not understand.

#!/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()

This is the error I'm getting:

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
melpomene
  • 84,125
  • 8
  • 85
  • 148
dynozor
  • 11
  • 3
  • `myString` is an integer. – Peter Wood Feb 04 '17 at 11:23
  • Shouldn't that be `raw_input`? – melpomene Feb 04 '17 at 11:26
  • 1
    Depending upon whether you're using Python 2 or 3 `input` behaves differently.. Python 2 `input` will evaluate the input string and convert it into an integer, if it's an integer. You must be using Python 2. If you don't want it evaluated you need to use `raw_input`. See answers to [Differences between `input` and `raw_input`](http://stackoverflow.com/questions/3800846/differences-between-input-and-raw-input) – Peter Wood Feb 04 '17 at 11:28
  • 1
    The code is just working fine when I give input as `11111` it gives me output as `Carte invalide`. You can check it [here](http://www.pythontutor.com/live.html#mode=edit) if you want. – Kedar Kodgire Feb 04 '17 at 11:29

2 Answers2

1

Depending upon whether you're using Python 2 or 3 input behaves differently.

Python 2 input will evaluate the input string and convert it into an integer, if it's an integer.

You are probably using Python 2.

If you don't want it evaluated you need to use raw_input.

See answers to Differences between input and raw_input

Community
  • 1
  • 1
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

First of all your function name says you try to sum digits. Then you wrote myString as parameter, and trying to find the length of that String. But I think you are passing Numbers to that function while calling and trying to find the length of that numbers but actually in python you can't find the length of numbers.

So your error is at line number 5.

Rethink on your logic.

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61