Python:Int() Base 10 的无效文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15993238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:32:21  来源:igfitidea点击:

Python: Invalid Literal for Int() Base 10

pythonliteralsbase

提问by Frontier

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])

这给了我一个 sumofodds 函数的错误,其中 j=int(card[i])

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

错误是“Int() 的字面值无效,基数为 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: '#'

采纳答案by Lennart Regebro

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. :-)

好吧,无论您输入了什么,实际上都不是 Base 10 数字。这包括任何不是数字字符或空格的内容。所以不要输入那个。:-)

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

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

更新:是的,您输入了“#”。那不是一个有效的数字。

回答by icktoofay

You're calling inputeach 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 inputin mainand should be passing cardas an argument to each of those other functions.

input每次进入sumofoddssumofevens、 或时都会调用sumofdoubles,因此他们每个人都将使用一个单独的信用卡号。你可能只需要被调用inputmain,应当通过card作为参数传递给每个这样的其他功能。

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)
        # ...