Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在DOB中添加所有数字?_Python 3.x - Fatal编程技术网

Python 3.x 在DOB中添加所有数字?

Python 3.x 在DOB中添加所有数字?,python-3.x,Python 3.x,如何将Dob中的所有数字相加?例如080789=32但在要求用户提供其DOB的函数中 我已经有了: me = int(input("enter your dob")) def dob(me): dob = [] count = 0 me = str(me) for i in range(len(me)): dob.append(me[i]) for i in range(len(dob)): dob[i] = int(do

如何将Dob中的所有数字相加?例如080789=32但在要求用户提供其DOB的函数中

我已经有了:

me = int(input("enter your dob"))

def dob(me):
    dob = []
    count = 0
    me = str(me)
    for i in range(len(me)):
        dob.append(me[i])
    for i in range(len(dob)):
        dob[i] = int(dob[i])
        count += dob[i]
    total = count + me
print(total)

代码中的
dob
函数从不被调用,并且
total
在函数外部打印,而没有从函数返回
total
。从我看到的情况来看,您还尝试直接将
输入
转换为
int
,然后在函数内部将其转换回
字符串
。这是不需要的

下面是一个简单的代码,可以实现您想要的功能

def dob(me):
    # Create a list of ints containing only the
    # numbers from the user input
    me = [int(s) for s in me if s.isdigit()]

    # Add all numbers in me and print it.
    total = sum(me)
    print(total)

# Get user input and store it to answer
answer = input("enter your dob")

# Call dob function and pass it the answer / input
dob(answer)

欢迎来到堆栈溢出!您似乎在要求某人为您编写一些代码。堆栈溢出是一个问答网站,而不是代码编写服务。请学习如何编写有效的问题。
sum(map(int,'080789'))
@senshin如果用户输入,例如,
08/07/89