with open("质数.txt", "w") as f: for a in range(2, 20): c = 0 b = 2 while not b > a: if a % b == 0: c = c + 1 b = b + 1 if c < 2: f.write(str(a)) f.write(',')
📖 质数定义: 只有1和它本身两个因数的自然数。
📘 9-加法表
with open("加法表.txt", "w") as f: for a in range(1, 10): for b in range(a, 10): t = str(a)+'+'+str(b)+'='+str(a+b) f.write(t+'\t') f.write('\n')
📖 转义字符: \n换行、\t制表符、\\反斜杠、\'单引号、\"双引号
📘 10-闰年
while 1: x = int(input('请输入年份:')) if x % 400 == 0: print(x,'年是闰年。') elif x % 4 == 0 and x % 100 != 0: print(x,'年是闰年。') else: print(x,'年不是闰年。')
📖 闰年规则: 能被400整除,或能被4整除但不能被100整除。
📘 11-星座查询器
while 1: x = float(input('请输入出生日期(如:3.12):')) if 3.21 <= x <= 4.19: print('白羊座') elif 4.20 <= x <= 5.20: print('金牛座') # ... 其他星座判断
📖 多分支语句: if...elif...else
星座
日期
星座
日期
白羊座
3.21-4.19
天秤座
9.23-10.23
金牛座
4.20-5.20
天蝎座
10.24-11.22
双子座
5.21-6.21
射手座
11.23-12.21
巨蟹座
6.22-7.22
摩羯座
12.22-1.19
📘 12-汉译英
d = {0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'} while 1: t = int(input('输入:')) print(d.get(t))
📖 字典: 字典用{}定义,键值对用冒号分隔,用get()方法安全访问。
📘 13-属相查询器
sx = ['猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊'] year = int(input('输入出生年份:')) print(f'{year}年属相:{sx[year%12]}')
📖 列表: 列表用[]定义,索引从0开始。
📘 14-六十花甲
tian = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸'] di = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'] with open("六十花甲.txt","w") as f: for i in range(60): f.write(tian[i%10]+di[i%12]+'\t')
📘 15-排序
l = [] for i in range(5): l.append(int(input('输入整数:'))) l.sort() print('递增:',l) l.sort(reverse=True) print('递减:',l)
📖 列表排序: sort()升序,sort(reverse=True)降序。
📘 16-口算训练器
import random while 1: a = random.randint(1,9) b = random.randint(1,9) print(f'{a}+{b}=?') c = int(input()) if c==a+b: print('正确!') else: print('错误!')
📘 17-随机出题器
import random, keyboard def chu(): a = random.randint(2,100) b = random.randint(2,a) print(f'{a}/{b}=?') keyboard.add_hotkey('space',chu) keyboard.wait()
from turtle import * colors=['red','green','yellow','blue','orange','pink'] pensize(5) for i in range(6): fd(200) dot(80,colors[i%6]) left(60) write("六色环",font=("楷体",28))
📘 20-奥运五环
from turtle import * colors=['blue','black','red','yellow','green'] pensize(12) for i in range(3): penup(); goto(i*180,0); pendown() pencolor(colors[i%5]); circle(80) penup(); goto(90,-90); pendown() for i in range(3,5): pencolor(colors[i%5]); circle(80) penup(); fd(180); pendown()
📘 21-图章和克隆
from turtle import * screensize(500,500) register_shape('行人.gif') shape('行人.gif') penup(); goto(-250,-250) for i in range(4): for a in range(6): clone(); fd(90) left(90)
from turtle import * setup(width=1800,height=1100) speed(10) while 1: a = int(numinput("边数","请输入正多边形边数:",3,minval=3,maxval=20)) clear() for i in range(a): fd(150); left(360/a)
📘 26-词频分析
import jieba txt = open("steel.txt","r",encoding='utf-8').read() words = jieba.lcut(txt) counts = {} for word in words: if len(word)==1: continue 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(f"{word:<10}{count:>5}")
# 加密 while 1: x = input('输入明文:') for ch in x: print(ord(ch)+20) # 解密 while 1: a = int(input('输入密文:')) print(chr(a-20))
📖 ord()和chr(): ord('a')返回97,chr(97)返回'a'
📘 30-猜拳
import random choices = {1:'剪刀',2:'石头',3:'布'} while 1: a = int(input('请出拳(1剪刀 2石头 3布):')) b = random.randint(1,3) print(f'你出{choices[a]},电脑出{choices[b]}') if a==b: print('平局') elif (a==1 and b==3) or (a==2 and b==1) or (a==3 and b==2): print('你赢了!') else: print('你输了!')
📘 31-T恤选择器
import random, keyboard txk = ['红','绿','橙','白','蓝','黄'] ls = 1 while 1: keyboard.wait('space') xc = random.randint(0,len(txk)-1) print(f'第{ls}轮:你选中的颜色是:{txk[xc]}') del txk[xc] if len(txk)==0: txk = ['红','绿','橙','白','蓝','黄'] ls += 1
📘 32-查询成绩
from openpyxl import load_workbook name = input('输入学生姓名:') wb1 = load_workbook('上学期成绩册.xlsx') st1 = wb1['Sheet1'] for row in range(1, st1.max_row+1): if st1.cell(row,1).value == name: print(f'上学期{name}成绩:{st1.cell(row,2).value}')