2015-12-07 31 views
-4

我該如何將所有數字添加到Dob中?例如; 080789 = 32,但在一個函數詢問用戶的DOB?添加所有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) 
+3

歡迎來到Stack Overflow!你似乎在要求某人爲你寫一些代碼。堆棧溢出是一個問答網站,而不是代碼寫入服務。請[see here](http://stackoverflow.com/help/how-to-ask)學習如何編寫有效的問題。 –

+0

'sum(map(int,'080789'))' – senshin

+0

@senshin如果用戶輸入'08/07/89' –

回答

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)