如何将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)
答案 0 :(得分:0)
从不调用代码中的dob
函数,并且在函数外部打印total
而不从函数返回total
。从我所看到的,您还尝试直接将input
转换为int
,然后将其转换回函数内的string
。这不是必需的。
这是一个可以做你想要的简单代码。
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)