写程序,可求m!+n!+k!


In [ ]:
def compute_sum(end):
    i = 1
    total_m = 1
    
    while end > i:
        i = i+1
        total_m = total_m * i
        
    return total_m

m = int(input('请输入第1个整数'))
n = int(input('请输入第2个整数'))
k = int(input('请输入第3个整数'))

print('最终的和是:', compute_sum(m) + compute_sum(n) + compute_sum(k))

写函数可返回1-1/3+1/5-1/7....的前n项和。在主程序中,分别令n=1000及100000,打印4倍该函数的和。


In [1]:
def compute_sum(end):
    t = 0
    i = 1
    total_n = 1
    
    while t < end:
        i = i + 2
        t = t + 1
        total_n = total_n + (-1)**t * 1/i
        
    return total_n

n = 1000
print('4倍前1000项的和为:', 4*compute_sum(n))
n = 100000
print('4倍前100000项的和为:', 4*compute_sum(n))


4倍前1000项的和为: 3.1425916543395442
4倍前100000项的和为: 3.1416026534897203

将task3中的练习1改写为函数,并进行调用


In [ ]:
name = input('请输入你的姓名,以回车结束')
print('你好', name)

def compute_star(d):
    if m == 4:
        if d < 20:
            j = '你是白羊座'
        else:
            j = '你是非常有性格的金牛座'
    
    return j

m = int(input('请输入你的出生月份'))
d = int(input('请输入你的出生日期'))

print(computer_star(d))

将task3中的练习4改写为函数,并进行调用


In [ ]: