In [ ]:
练习1
In [ ]:
def factorial(number):
i=0
while i !=1:
i=number-1
multiply=number*i
return multiply
m=input('请输入一个数,回车结束')
print('结果是:',factorial(m))
In [ ]:
练习2
In [3]:
def total_n(end):
i=0
total_n=0
while i<end:
i=i+1
total_n=total_n+(-1)**(i-1)/(2*i-1)
return total_n
n=1000
m=10000
print('结果是:',4*total_n(n),4*total_n(m))
In [ ]:
Task 3 练习一
In [35]:
def constellation(m,n):
n!=0
m!=0
if n==1:
if m<=18:
print('你是非常有性格的摩羯座')
else:
print('你是非常有性格的水瓶座')
elif n==2:
if m<=18:
print('你是非常有性格的水瓶座')
else:
print('你是非常有性格的双鱼座')
elif n==3:
if m<=20:
print('你是非常有性格的双鱼座')
else:
print('你是非常有性格的白羊座')
elif n==4:
if m<=19:
print('你是非常有性格的白羊座')
else:
print('你是非常有性格的金牛座')
elif n==5:
if m<=20:
print('你是非常有性格的金牛座')
else:
print('你是非常有性格的双子座')
elif n==6:
if m<=21:
print('你是非常有性格的双子座')
else:
print('你是非常有性格的巨蟹座')
elif n==7:
if m<=23:
print('你是非常有性格的巨蟹座')
else:
print('你是非常有性格的狮子座')
elif n==8:
if m<=22:
print('你是非常有性格的狮子座')
else:
print('你是非常有性格的处女座')
elif n==9:
if m<=23:
print('你是非常有性格的处女座')
else:
print('你是非常有性格的天秤座')
elif n==10:
if m<=24:
print('你是非常有性格的天秤座')
else:
print('你是非常有性格的天蝎座')
elif n==11:
if m<=23:
print('你是非常有性格的天蝎座')
else:
print('你是非常有性格的射手座')
elif n==12:
if m<=22:
print('你是非常有性格的射手座')
else:
print('你是非常有性格的摩羯座')
return constellation(m,n)
m=int(input(''))
n=int(input(''))
print(constellation(m,n))
In [ ]:
Task 3 练习四
In [3]:
def plural(word):
if word.endswith ('x'):
print(word,'es',sep='')
elif word.endswith ('sh'):
print(word,'es',sep='')
else:
print(word,'s',sep='')
m=input('请输入一个单词,回车结束')
plural(m)
In [ ]:
挑战性练习
In [1]:
def sum(a,b):
if a<=b:
total=0
while a<=b:
total=total+a
a+=1
else:
total=0
while a>=b:
total=total+b
b+=1
return total
m=int(input('请输入一个整数,回车结束'))
k=int(input('请输入另一个整数,为间隔数,回车结束'))
n=(m+k)
sum(m,n)
Out[1]: