MOOC嵩天python期末测试编程题汇总

xiaoxiao2021-02-28  4

2-1 快乐数字

def snum(num): strnum = str(num) sum = 0 b = [int(x) for x in strnum] for i in b: sum += i**2 return sum def main(): a = input() num = eval(a) count = 0 while num != 1: num = snum(num) count += 1 if count > 2000: print("False") break else: print("True") main()

2-2数列阶乘相加

a = input() sum = 0 if a.isdigit(): num = eval(a) if num > 0: count = 1 for i in range(1, num+1): count *= i sum += count print(sum) else: print("输入有误,请输入正整数") else: print("输入有误,请输入正整数")

2-3成绩转换输入

try:     a = eval(input()) except:     print('输入数据有误!') else:     if 0 <= a < 60:         print('输入成绩属于E级别。')     elif 60 <= a < 70:         print('输入成绩属于D级别。')         print('祝贺你通过考试!')     elif 70 <= a < 80:         print('输入成绩属于C级别。')         print('祝贺你通过考试!')     elif 80 <= a < 90:         print('输入成绩属于B级别。')         print('祝贺你通过考试!')     elif 90 <= a <= 100:         print('输入成绩属于A级别。')         print('祝贺你通过考试!')     else:         print('输入数据有误!') finally:     print('好好学习,天天向上!')

3-1斐波那契数列计算B

def fblq(n): if n == 0: m = 0 elif n == 1: m = 1 else: m = fblq(n - 1) + fblq(n - 2) return m def main(): a = eval(input()) sum = 0 fblq_list = [] for i in range(0, a+1): print('{}, '.format(fblq(i)),end='') sum += fblq(i) equal = sum / (a+1) print('{}, {:.0f}'.format(sum, equal)) main()

3.2-站队顺序输出

from operator import itemgetter queue = eval(input()) queue.sort(key = itemgetter(1)) #print(queue) queue.sort(key = itemgetter(0), reverse = True) #print(queue) output = [] for item in queue: output.insert(item[1], item) #print(output) print(output)

3.3-合法括号组合生

# 排列组合的问题,但是真的不容易实现,参考了https://blog.csdn.net/gaozhenweigzw/article/details/44781705 def PLZH(lst, ch, a, b): if (a == 0) and (b == 0): lst.append(ch) return(lst) if a > 0: PLZH(lst, ch + '(', a-1,b + 1) if b > 0: PLZH(lst, ch + ')', a, b - 1) a = eval(input()) b = 0 lst = [] ch = '' PLZH(lst, ch, a, b) print(lst)

3.4-用户登录(三次机会)

count = 0 for i in range (0, 3): count += 1 judge_n = input() judge_c = input() if judge_n == "Kate" and judge_c == "666666": print("登录成功!") bool = False break if bool: print("3次用户名或者密码均有误!退出程序。")

期末考试(比测验简单多了)

凯撒密码B

PassInit = input() for ind in PassInit: if (ord('a') <= ord(ind) <= ord('z')): PassKaisa = chr(ord('a')+(ord(ind)-ord('a')+3)&) print(PassKaisa,end='') elif (ord('A') <= ord(ind) <= ord('Z')): PassKaisa = chr(ord('A')+(ord(ind)-ord('A')+3)&) print(PassKaisa,end='') else: PassKaisa = ind print(PassKaisa,end='') continue

水仙花数

sxhnum = [] for i in range (100, 1000): A = i // 100 B = i // 10 % 10 C = i % 10 if A**3 + B**3 + C**3 == i: sxhnum.append(i) else: continue len_num = len(sxhnum) for j in range (len_num): if j < len_num - 1: print('{},'.format(sxhnum[j]),end='') else: print('{}'.format(sxhnum[j]))

心里话

这题有问题直接  print("李安,我想对你说,你真有才!")  就 AC 了。

a = input() b = input() str1 = a + ',我想对你说,' + b print(str1)

字符串垂直输出

str = input() for i in str: print(i)

哈姆雷特词频统计

def getText(): txt = open("hamlet.txt", "r").read() txt = txt.lower() for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~': txt = txt.replace(ch, " ") return txt hamletTxt = getText() words = hamletTxt.split() counts = {} for word in words: counts[word] = counts.get(word,0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) for i in range(10): word, count = items[i] print ("{:<10},{:>5}".format(word, count))

转载请注明原文地址: https://www.6miu.com/read-2400368.html

最新回复(0)