Question

I'm writing code for a project to determine the validity of credit cards and i've hit a wall, it seems like all of the things i have tried so far are not working.

This is giving me an error for the sumofodds function where j=int(card[i])

The error is "Invalid Literal for Int() with Base 10

Is there anyone that can give me some advce?

def sumofdoubles():
    card=input()
    x=len(card)
    summ=0

    for i in range(x-2,-1,-2):
        j=int(card[i])
        u=j+j

        if u>9:
            h=u/2
            summ=summ+h

     return(summ)

def sumofevens():
    card=input()
    x=len(card)
    summ=0

    for i in range(x-2,-1,-2):
        j=int(card[i])
        u=j+j
        if u<9:
            summ=summ+u

    return(summ)


def sumofodds():
    summ=0
    card=input()
    x=len(card)

    for i in range(x-1,-1,-2):
        j=int(card[i])
        summ=summ+j

    return(summ)

def main():
    card=input()
    length=len(card)
    summ=0

    while(card!="#####"):
        if (card[0]=='4' or card[0]=='5' or card[0]=='6' or (card[0]=='3' and      card[1]=='1')):
            dbls=sumofdoubles()
            evens=sumofevens()
            odds=sumofodds()
            if((dbls+evens+odds)%10==0):
                print("Valid")

main()

This is the full traceback for those wondering

    python test.py<s.input
    File "test.py", line 52 in <module>
      main()
    File "test.py", line 48, in main
      odds=sumofodds()
    File "test.py", line 33, in sumofodds
      j=int(card[i])
ValueError: invalid literal for int() with base 10: '#'
Was it helpful?

Solution

Well, whatever you did you typed in something that isn't actually a Base 10 number. This includes anything that isn't number characters or spaces. So don't type in that. :-)

Examples:

>>> int('04.9')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '04.9'

>>> int('4-')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '4-'

>>> int("Jack")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Jack'

Update: Yes you typed a '#'. That's not a valid number.

OTHER TIPS

You're calling input each time you go into sumofodds, sumofevens, or sumofdoubles, so each of them will be working on a separate credit card number. You probably only want to be calling input in main and should be passing card as an argument to each of those other functions.

Your functions then might look something like this:

def sum_of_odds(card):
    x = len(card)
    # ...

# ...

def main():
    while True:
        card = input()
        if card == '#####':
            break
        odds = sum_of_odds(card)
        # ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top