Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Invalid Literal for Int() Base 10

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: '#'
like image 376
Frontier Avatar asked Apr 13 '13 21:04

Frontier


People also ask

What is invalid literal for int () with base 10 in Python?

invalid literal for int() with base 10. The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.

How do I fix this ValueError invalid literal for int with base 10 error in Python?

ValueError: invalid literal for int() with base 10 occurs when you convert the string or decimal or characters values not formatted as an integer. To solve the error, you can use the float() method to convert entered decimal input and then use the int() method to convert your number to an integer.

How do I convert a string to an int in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .

How do you convert a float to an int in Python?

Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer.


2 Answers

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.

like image 81
Lennart Regebro Avatar answered Oct 02 '22 23:10

Lennart Regebro


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)
        # ...
like image 28
icktoofay Avatar answered Oct 02 '22 22:10

icktoofay