Python:Int()Base 10的无效字面量 [英] Python: Invalid Literal for Int() Base 10

查看:148
本文介绍了Python: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.

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

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

错误是以10为底的Int()的文字无效

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: '#'

推荐答案

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

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

示例:

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

这篇关于Python:Int()Base 10的无效字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆