In [1]:
#练习 1:仿照求$ \sum_{i=1}^mi + \sum_{i=1}^ni  + \sum_{i=1}^ki$的完整代码,写程序,可求m!+n!+k!
def computer_sum(num):
    i = 1
    total = 1
    while i <= num:
        total *= i
        i += 1
    return total

m = int(input('请输入第一个整数,回车结束'))
n = int(input('请输入第二个整数,回车结束'))
k = int(input('请输入第三个整数,回车结束'))
print ('tatol =',computer_sum(m)+computer_sum(n)+computer_sum(k))


请输入第一个整数,回车结束3
请输入第二个整数,回车结束4
请输入第三个整数,回车结束5
tatol = 150

In [2]:
#练习 2:写函数可返回1 - 1/3 + 1/5 - 1/7...的前n项的和。在主程序中,分别令n=1000及100000,打印4倍该函数的和。
def computer_add(num):
    i = 0
    total = 0
    while i < num:
        j = 2*i + 1
        i +=1
        if i%2 != 0:
            total += 1/j
        else:
            total -= 1/j
    return total

m = int(input('请输入第一个整数,回车结束  '))
print ('4倍的和:tatol1 =',4*computer_add(m))
n = int(input('请输入第二个整数,回车结束  '))
print ('4倍的和:tatol2 =',4*computer_add(n))


请输入第一个整数,回车结束  1000
4倍的和:tatol1 = 3.140592653839794
请输入第二个整数,回车结束  10000
4倍的和:tatol2 = 3.1414926535900345

In [3]:
#练习 3:将task3中的练习1及练习4改写为函数,并进行调用。
#练习 1:写程序,可由键盘读入用户姓名例如Mr. right,让用户输入出生的月份与日期,判断用户星座,
#假设用户是金牛座,则输出,Mr. right,你是非常有性格的金牛座!。
def constellation(name,birthday_month,birthday_day):
    if birthday_month > 12 or birthday_month < 1 or birthday_day < 1 or birthday_day > 31:
        print ('你输入的数字非法!')
    if (birthday_month == 3 and 21<=birthday_day<=31) or (birthday_month == 4 and 1<=birthday_day<=19):
        print (name,'你是非常有性格的白羊座!',sep = ',')
    elif (birthday_month == 4 and 20<=birthday_day<=30) or (birthday_month == 5 and 1<=birthday_day<=20):
        print (name,'你是非常有性格的金牛座!',sep = ',')
    elif (birthday_month == 5 and 21<=birthday_day<=31) or (birthday_month == 6 and 1<=birthday_day<=21):
        print (name,'你是非常有性格的双子座!',sep = ',')
    elif (birthday_month == 6 and 22<=birthday_day<=30) or (birthday_month == 7 and 1<=birthday_day<=22):
        print (name,'你是非常有性格的巨蟹座!',sep = ',')
    elif (birthday_month == 7 and 23<=birthday_day<=31) or (birthday_month == 8 and 1<=birthday_day<=22):
        print (name,'你是非常有性格的狮子座!',sep = ',')
    elif (birthday_month == 8 and 23<=birthday_day<=31) or (birthday_month == 9 and 1<=birthday_day<=22):
        print (name,'你是非常有性格的处女座!',sep = ',')
    elif (birthday_month == 9 and 23<=birthday_day<=30) or (birthday_month == 10 and 1<=birthday_day<=23):
        print (name,'你是非常有性格的天秤座!',sep = ',')
    elif (birthday_month == 10 and 24<=birthday_day<=31) or (birthday_month == 11 and 1<=birthday_day<=22):
        print (name,'你是非常有性格的天蝎座!',sep = ',')
    elif (birthday_month == 11 and 23<=birthday_day<=30) or (birthday_month == 12 and 1<=birthday_day<=21):
        print (name,'你是非常有性格的射手座!',sep = ',')
    elif (birthday_month == 12 and 22<=birthday_day<=31) or (birthday_month == 1 and 1<=birthday_day<=19):
        print (name,'你是非常有性格的魔蝎座!',sep = ',')
    elif (birthday_month == 1 and 20<=birthday_day<=31) or (birthday_month == 2 and 1<=birthday_day<=18):
        print (name,'你是非常有性格的水瓶座!',sep = ',')
    elif (birthday_month == 2 and 19<=birthday_day<=28) or (birthday_month == 3 and 1<=birthday_day<=20):
        print (name,'你是非常有性格的双鱼座!',sep = ',')
        
name = input('请输入你的姓名,回车结束  ')
birthday_month = int(input('请输入你生日的月份,回车结束  '))
birthday_day = int(input('请输入你生日的日号,回车结束  '))
constellation(name,birthday_month,birthday_day)


请输入你的姓名,回车结束  lll
请输入你生日的月份,回车结束  4
请输入你生日的日号,回车结束  5
lll,你是非常有性格的白羊座!

In [4]:
#练习 3:将task3中的练习1及练习4改写为函数,并进行调用。
#练习 4:英文单词单数转复数,要求输入一个英文动词(单数形式),能够得到其复数形式,或给出单数转复数形式的建议
#(提示,some_string.endswith(some_letter)函数可以判断某字符串结尾字符)。

#写成英语名词了,就不写动词了

def sin_to_plu(Eng_word):
    if Eng_word.endswith('s') or Eng_word.endswith('x') or Eng_word.endswith('sh') or Eng_word.endswith('ch'):
        print (Eng_word+'es')
    elif Eng_word.endswith('y'):
        print (Eng_word[0:len(Eng_word)-1]+'ies')
    elif Eng_word.endswith('f') or Eng_word.endswith('fe'):
        if Eng_word == 'safe' or Eng_word == 'roof': #这特殊情况能列举完吗???
            print (Eng_word+'s')
        elif(Eng_word.endswith('f')):
            print (Eng_word[0:len(Eng_word)-1]+'ves')
        else :
            print (Eng_word[0:len(Eng_word)-2]+'ves')
    elif Eng_word.endswith('o'):
        if Eng_word == 'tomato' or Eng_word == 'tomato' or Eng_word == 'hero' or Eng_word == 'negro':  #这特殊情况能列举完吗???
            print (Eng_word+'es')
        else :
            print (Eng_word+'s')
    #elif #不规则的单词 就不写了
    else :
        print (Eng_word+'s')
        
while True :
    Eng_word = input('请输入一个要转换的英语单词,回车结束,输入‘q’结束  ')
    if Eng_word == 'q':
        break
    sin_to_plu(Eng_word)


请输入一个要转换的英语单词,回车结束,输入‘q’结束  book
books
请输入一个要转换的英语单词,回车结束,输入‘q’结束  live
lives
请输入一个要转换的英语单词,回车结束,输入‘q’结束  fish
fishes
请输入一个要转换的英语单词,回车结束,输入‘q’结束  q

In [5]:
#挑战性练习:写程序,可以求从整数m到整数n累加的和,间隔为k,求和部分需用函数实现,主程序中由用户输入m,n,k调用函数验证正确性。
def computer_sum(m,n,k):
    while m <= n:
        m += k
    return m

m = int(input('请输入第一个整数即m,回车结束  '))
n = int(input('请输入第二个整数即n,回车结束  '))
k = int(input('请输入第三个整数即k,回车结束  '))
print ('tatol =',computer_sum(m,n,k))


请输入第一个整数即m,回车结束  2
请输入第二个整数即n,回车结束  12
请输入第三个整数即k,回车结束  3
tatol = 14