In [1]:
#练习1
def compute_pro(end):
    i = 1
    factor_n = 1

    while i < end:
        i = i + 1
        factor_n = factor_n*i

    return factor_n

n = int(input('请输入第1个整数,以回车结束。'))
m = int(input('请输入第2个整数,以回车结束。'))
k = int(input('请输入第3个整数,以回车结束。'))

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


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

In [3]:
#练习2
def compute_sum(end):
    import math
    n=0
    i = 1
    total_n = 0
    while n < end:
        total_n = total_n + 1/i*math.pow(-1,n)
        i = i + 2
        n=n+1
    return total_n
print(' ', compute_sum(1000))
print(' ', compute_sum(100000))


  0.7851481634599485
  0.7853956633974299

In [4]:
#练习3 1
def birthday():
    x=int(input('Enter the date of your birthday'))
    return x
if 520>=birthday()>=420  :
    print('Mr. right,你是非常有性格的金牛座!')
else:
    print('Mr. right,你不是金牛座。')


Enter the date of your birthday520
Mr. right,你是非常有性格的金牛座!

In [5]:
#练习3 2
def operation(m,n):
    operation=input('What do you want to do with the two numbers?')
    if operation.endswith('plus'):
        sum=m+n
        return sum
    else:
        div=n/m
        return div
m = int(input('Enter an integer:'))
n = int(input('Enter another integer,and it is not zero'))
print(' ',operation(m,n))


Enter an integer:5
Enter another integer,and it is not zero6
What do you want to do with the two numbers?plus
  11

In [6]:
#练习3 3
def action(num):
    if num>500:
        print('应该打开空气净化器,戴防雾霾口罩')
    else :
        print('It is no problem')
num = int(input('Enter the PM2.5 value: '))
action(num)


Enter the PM2.5 value: 500
It is no problem

In [7]:
#练习3 4
def pro(word):
    if word.endswith('y'):  
        print('变y为i加es') 
    elif word[-1] in 'sx' or word[-2:] in ['sh','ch']:  
        print('加es')  
    else:  
        print('加s')
word=input('Enter a noun: ')
pro(word)


Enter a noun: plus
加es

In [8]:
#挑战
def total(m,n,k):
    total=0
    i=0
    t=(n-m)/k+1
    while i<t:
        total=total+m
        m=m+k
        i=i+1
    return total
m = int(input('请输入第1个整数,以回车结束。'))
n = int(input('请输入第2个整数,以回车结束。'))
k = int(input('请输入第3个整数,以回车结束。'))
total(m,n,k)


请输入第1个整数,以回车结束。2
请输入第2个整数,以回车结束。8
请输入第3个整数,以回车结束。2
Out[8]:
20

In [ ]:


In [ ]: