In [3]:
#练习 1:仿照求 ∑mi=1i+∑ni=1i+∑ki=1i∑i=1mi+∑i=1ni+∑i=1ki 的完整代码,写程序,可求m!+n!+k!
n = int(input('请输入第1个整数,以回车结束。'))
i = 0
total_n = 1
while i < n:
    i = i + 1
    total_n = total_n*i

m = int(input('请输入第2个整数,以回车结束。'))
i = 0
total_m = 1
while i < m:
    i = i + 1
    total_m = total_m*i

k = int(input('请输入第3个整数,以回车结束。'))
i = 0
total_k = 1
while i < k:
    i = i + 1
    total_k = total_k*i

print('最终的和是:', total_n + total_m + total_k)


请输入第1个整数,以回车结束。3
请输入第2个整数,以回车结束。4
请输入第3个整数,以回车结束。5
最终的和是: 150

In [6]:
#练习 2:写函数可返回1-1/3+1/5-1/7...的前n项的和。在主程序中,分别令n=1000及100000,打印4倍该函数的和。
import math
def qiuhe(n):
    sum=0
    i=0
    y=0
    while i<n:
        i=i+1
        sum=sum+pow(-1,y)*(1/(y+1))
        y=y+2
    return sum
print(pow(qiuhe(1000),4))
print(pow(qiuhe(10000),4))


387.09921934852457
974.2971391280846

In [26]:
#练习 3:将task3中的练习1改写为函数,并进行调用
def star(m,n):
    if (m==3 and n>=21)or(m==4 and n<=19):
        print('你是率真坦白的白羊座')
    if (m==4 and n>=20)or(m==5 and n<=20):
        print('你是你是非常有性格的金牛座')
    if (m==5 and n>=21)or(m==6 and n<=21):
        print('你是精灵鬼怪的双子座')
    if (m==6 and n>=22)or(m==7 and n<=22):
        print('你是温柔有爱心的巨蟹座')
    if (m==7 and n>=23)or(m==8 and n<=22):
        print('你是有王者气质的狮子座')
    if(m==8 and n>=23)or(m==9 and n<=22):
        print('你是追求完美的处女座')
    if (m==9 and n>=23)or(m==10 and n<=23):
        print('你是爱好和平的天秤座')
    if (m==10 and n>=24)or(m==11 and n<=22):
        print('你是心思缜密的天蝎座')
    if (m==11 and n>=23)or(m==12 and n<=21):
        print('你是热爱自由的射手座')
    if (m==12 and n>=22)or(m==1 and n<=19):
        print('你是脚踏实地的摩羯座')
    if (m==1 and n>=20)or(m==2 and n<=18):
        print('你是理性独立的水瓶座')
    if (m==2 and n>=19)or(m==3 and n<=20):
        print('你是浪漫温柔的双鱼座')

x=input('请输入你的名字')
a=int(input('请输入你的出生月份'))
b=int(input('请输入你的出生日子'))
print(x)
star(a,b)


请输入你的名字yyy
请输入你的出生月份8
请输入你的出生日子22
yyy
你是有王者气质的狮子座

In [31]:
#练习 3:将task3中的练习4改写为函数,并进行调用
def change(word):  
    if word.endswith('y'):  
        return word[:-1] + 'ies'  
    elif word[-1] in ('s'or'x') or word[-2:] in ['sh', 'ch']:  
        return word + 'es'  
    elif word.endswith('f') or word[-2:] in ['fe']:  
        return word[:-2] + 'ves'  
    else:  
        return word + 's'
x=input('输入单词')
print(change(x))


输入单词shich
shiches

In [35]:
#写程序,可以求从整数m到整数n累加的和,间隔为k,求和部分需用函数实现,主程序中由用户输入m,n,k调用函数验证正确性。
def qiuhe(m,n,k):
    sum=0
    while m<=n:
        sum=m+sum
        m=m+k
    return sum
a=int(input('请输入一个整数'))
b=int(input('请输入另一个整数'))
c=int(input('请输入他们之间的间隔'))
print(qiuhe(a,b,c))


请输入一个整数10
请输入另一个整数100
请输入他们之间的间隔10
550