第六周练习
Posted slj-xt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六周练习相关的知识,希望对你有一定的参考价值。
1、水仙花数
n = int(input()) if n>=3 and n<6: for i in range(10**(n-1),10**n): sum=0 for j in map(int,str(i)): sum=sum+j**n if sum==i: print(i)
2、数列求和
a = int(input()) #接收用户输入并转为整数 n = int(input()) if 9 >= a >= 0: #当n在1-9间时计算 tmp = a sum = 0 #赋初值0 for i in range(n): sum = sum + tmp #求和 tmp = tmp * 10 + a print(sum)
3、温度转换异常处理
try: TempStr = input() if TempStr[-1] in [‘F‘, ‘f‘]: C = (eval(TempStr[0:-1]) - 32) / 1.8 print("{:.2f}C".format(C)) elif TempStr[-1] in [‘C‘, ‘c‘]: F = 1.8 * eval(TempStr[0:-1]) + 32 print("{:.2f}F".format(F)) else: print("输入错误,末位只能是‘C‘,‘c‘,‘F‘,‘f‘") except NameError: print(‘试图访问的变量名不存在‘) except SyntaxError: print(‘存在语法错误‘) except Exception as e: print(e)
4、判断是否直角三角形
a=eval(input()) b=eval(input()) c=eval(input()) if(a**2+b**2==c**2 or b**2+c**2==a**2 or a**2+c**2==b**2): print("YES") else: print("NO")
5、求e的近似值
import math n = eval(input()) e = 1 for i in range(1,n+1): e += 1 / (math.factorial(i)) print(‘{:.8f}‘.format(e))
6、计算圆周率
threshold = eval(input()) pi4 = k = 0 f = 1 while abs(1 / (2 * k + 1)) >= threshold: pi4 = pi4 + f * 1 / (2 * k + 1) k = k + 1 f = -f print("{:.6f}".format(pi4*4))
7、个税计算器
s = float(input()) if s <0: print("error") else: salary = s - 5000 if salary <=3000: fee, num = 3,0 elif 3000 < salary <= 12000: fee, num = 10, 210 elif 12000 < salary <= 25000: fee, num = 20, 1410 elif 25000 < salary <= 35000: fee, num = 25, 2660 elif 35000 < salary <= 55000: fee, num = 30, 4410 elif 55000 < salary <= 80000: fee, num = 35, 7160 elif 80000 < salary : fee, num = 45, 15160 tax = salary * fee / 100 - num print("应缴税款{:.2f}元,实发工资{:.2f}元。".format(tax,salary + 5000 - tax))
8、身份证号校验
ls = ( 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ) id = input() sum = 0 for i in range(17): sum = sum + ls[i] * int(id[i]) if str(id[17]) == ‘X‘: if sum % 11 == 2: print(‘身份证号码校验为合法号码!‘) else: print(‘身份证校验位错误!‘) elif (sum % 11 + int(id[17])) % 11 == 1: print(‘身份证号码校验为合法号码!‘) else: print(‘身份证校验位错误!‘)
9、校验身份证号码并输出个人信息
mport datetime ls = ( 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ) id = input() def IDcheck(id): sum = 0 for i in range(17): sum = sum + ls[i] * int(id[i]) if id[17] == ‘X‘: if sum % 11 == 2: return 1 else: return 0 elif (sum % 11 + int(id[17])) % 11 == 1: return 1 else: return 0 if IDcheck(id) == 1: year = id[6:10] month = id[10:12] day = id[12:14] if int(id[16]) % 2 == 0: sex = ‘女‘ if int(id[16]) % 2 == 1: sex = ‘男‘ print(‘身份证号码校验为合法号码!‘) print(‘出生:{}年{}月{}日‘.format(year, month, day)) print(‘年龄:{}‘.format(datetime.datetime.now().year - int(year))) print(‘性别:{}‘.format(sex)) else: print(‘身份证校验位错误!‘)
10、模拟生成微软序列号
import random def key(num): for i in range(num): keySn = [] for j in range(5): s = ‘‘ characters = ‘BCEFGHJKMPQRTVWXY2346789‘ for j in range(5): s = s + random.choice(characters) keySn.append(s) key = ‘-‘.join(keySn) print(key) num = int(input()) n = input() random.seed(n) key(num)
以上是关于第六周练习的主要内容,如果未能解决你的问题,请参考以下文章