① 数字炸弹 💣(1-100 猜数字)
import random
bomb = random.randint(1, 100)
print("💣 数字炸弹 1-100 已安置!")
while (guess := int(input("👉 猜数字:"))) != bomb:
print("📈 小了" if guess < bomb else "📉 大了")
print(f"💥 被炸毁!你用了 {print(1)} 次") # 彩蛋:print 返回值
② 石头剪刀布 ✊✋✌️(三局两胜)
import random, sys
rps = "✊✋✌️"
win = 0
for i in range(3):
me = int(input(f"{i+1}/3 0=石头 1=剪刀 2=布:"))
bot = random.randint(0, 2)
print(f"你{rps[me]} vs 电脑{rps[bot]}")
win += (me - bot) % 3 == 1
print("🎊 你赢!" if win >= 2 else "🤖 电脑赢!")
③ 打字竞速 🏃♂️(10 词计时)
import random, time, sys
words = ["python", "java", "code", "speed", "fun"]
random.shuffle(words)
start = time.time()
for w in words[:5]:
print(w); sys.stdin.readline()
print(f"🎯 速度:{len(words)/(time.time()-start)*60:.1f} 词/分")
④ 终端彩色贪吃蛇 🐍(核心逻辑)
import random, os, tty, termios, select
W,H,s,xy=20,10,"🟩🟥🟦",lambda x,y:f"\033[{y};{x}H"
b=[[x,0]for x in range(4)];d=(1,0);f=[random.randint(1,W-2),random.randint(1,H-2)]
def game():
global d,f,fd;fd=termios.tcgetattr(sys.stdin);tty.setraw(sys.stdin.fileno())
print("\033[2J\033[H",end="");print(s[2]*(W+2));[print(s[2]+" "*W+s[2])for _ in range(H)];print(s[2]*(W+2))
while True:
if select.select([sys.stdin],[],[],0.1)[0]:
k=sys.stdin.read(1);d={(1,0):k=='d',(-1,0):k=='a',(0,-1):k=='w',(0,1):k=='s'}.get(d,d)
h=[b[0][0]+d[0],b[0][1]+d[1]];h[0]%=W;h[1]%=H
if h in b:break
b.insert(0,h);print(xy(*f)+s[1]);print(xy(*h)+s[0])
if h==f:f=[random.randint(1,W-2),random.randint(1,H-2)];print(xy(*f)+s[1])
else:b.pop();print(xy(*b[-1][0],*b[-1][1])+" ")
if __name__ == "__main__":
import sys
try:game()
except:pass
finally:termios.tcsetattr(sys.stdin,termios.TCSADRAIN,fd);print("\033[0m")
运行:
python snake.py
控制:W A S D 移动,撞墙/咬自己 GameOver!
⑤ 命令行天气预报 🌤️(内置库 only)
import urllib.request, json, sys
city = sys.argv[1] if len(sys.argv) > 1 else "Beijing"
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
data = json.loads(urllib.request.urlopen(url).read())
today = data["data"]["forecast"][0]
print(f"{city} 今天:{today['type']} {today['low']} ~ {today['high']}")
⑥ 二维码终端打印 📱(内置库)
import qrcode # 仅需 pip install qrcode[pil]
qr = qrcode.QRCode(border=1)
qr.add_data("https://github.com/你的名")
qr.print_ascii() # 黑白块直接打印
🏁 一键运行
python 文件名.py