我想制作一个python应用程序,用来计算生日的数字。
day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
day = int(day)
month = int(month)
year = int(year)假设某人生于1928年12月10日,我怎么能让这个应用程序概括成这样: 1+2+1+0+1+9+2+8?这是在Python2.7上完成的
PS:我忘记提到,我的最后结果必须是一个1位数,例如,在上面的结果之和是24,但是这个数字本身必须是2+4= 6,最后答案是6。
发布于 2016-05-02 03:32:31
与其将它们转换为整数,不如将它们添加为字符串,然后将每个字符映射为整数,然后查找和:
day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
print sum(map(int, day+month+year))如果要一直添加数字,直到得到一个1位数字,请使用循环:
day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
num = day+month+year
while len(num) > 1:
num = str(sum(map(int, num)))
print num发布于 2016-05-02 04:19:44
下面的函数将为任何输入数字提供以单位数字和的输出。只需键入您的日,月,年,把它们加起来,并提供以下功能的输入。
def get_sum_as_single_digit(digit):
final_sum=0
while digit>0:
mod = digit%10
digit = digit/10
final_sum = final_sum + mod
if final_sum>9:
return get_single_digit_sum(final_sum)
else:
return final_sumhttps://stackoverflow.com/questions/36974588
复制相似问题