3 回答

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
可以將字符串視為列表。因此,如果您將天+月+年的總數(shù)變成一個(gè)字符串,然后循環(huán)遍歷它就可以了
print('Welcome to BirthPath Calculator')
day = int(input('Input you day of Birth (1-31): '))
month = int(input('Input your month of birth (1-12): '))
year = int(input('Input your year of birth: '))
total = day + month + year
birthpath = 0
for digit in str(total):
birthpath += int(digit)
print('This is your Birth Path: ' + str(birthpath))
您還可以使用列表理解來(lái)縮短它的時(shí)間。
print('Welcome to BirthPath Calculator')
day = int(input('Input you day of Birth (1-31): '))
month = int(input('Input your month of birth (1-12): '))
year = int(input('Input your year of birth: '))
total = day + month + year
birthpath = sum(int(digit) for digit in str(total))
print('This is your Birth Path: '+str(birthpath))

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
這個(gè)簡(jiǎn)單的一個(gè)襯墊會(huì)起作用!
Birthpath = 2014
sum(int(i) for i in str(Birthpath))

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
得到一個(gè)數(shù)字的數(shù)字和是一個(gè)古老的經(jīng)典問(wèn)題:
def sum_of_digits(number):
sum = 0
while number > 0:
remainder = number % 10
sum += remainder
number //= 10
return sum
添加回答
舉報(bào)