3 回答
TA贡献2041条经验 获得超4个赞
可以将字符串视为列表。因此,如果您将天+月+年的总数变成一个字符串,然后循环遍历它就可以了
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))
您还可以使用列表理解来缩短它的时间。
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贡献1998条经验 获得超6个赞
得到一个数字的数字和是一个古老的经典问题:
def sum_of_digits(number):
sum = 0
while number > 0:
remainder = number % 10
sum += remainder
number //= 10
return sum
添加回答
举报