python
Copy
# 温度预警示例
temperature = float(input("当前环境温度:"))
if temperature > 35:
print("高温红色预警!建议避免户外活动")
python
Copy
# 文件格式验证增强版
file_extension = input("请输入文件后缀名:").lower()
if file_extension in ['jpg', 'png', 'gif']:
print("支持处理的图像格式")
else:
print("错误:不支持的格式,请转换后再上传")
python
Copy
# 考试成绩评级系统(优化版)
score = float(input("请输入考试成绩:"))
if score >= 95:
grade = "A+"
elif 90 <= score < 95:
grade = "A"
elif 85 <= score < 90:
grade = "B+"
elif 75 <= score < 85:
grade = "B"
elif 60 <= score < 75:
grade = "C"
else:
grade = "D(不及格)"
print(f"成绩等级:{grade}")
python
Copy
# 快速判断奇偶性
num = int(input("输入整数:"))
result = "偶数" if num % 2 == 0 else "奇数"
print(result)
python
Copy
# 机场安检系统模拟
has_ticket = input("是否有机票(Y/N)?").upper() == 'Y'
security_check = input("通过安检了吗(Y/N)?").upper() == 'Y'
if has_ticket:
print("进入候机区域")
if security_check:
print("允许登机")
else:
print("请重新进行安检!")
else:
print("请先办理值机手续")
python
Copy
# 密码验证加强版
MAX_ATTEMPTS = 3
attempts = 0
stored_password = "secure@123"
while attempts < MAX_ATTEMPTS:
user_input = input("请输入密码:")
if user_input == stored_password:
print("验证成功!")
break
else:
attempts += 1
remaining = MAX_ATTEMPTS - attempts
print(f"密码错误,剩余尝试次数:{remaining}")
else:
print("账户已锁定,请联系管理员")
python
Copy
# 九九乘法表生成器
row = 1
while row <= 9:
col = 1
while col <= row:
print(f"{col}x{row}={col*row}", end='\t')
col += 1
print()
row += 1
python
Copy
# 文本分析工具
sentence = "Python programming is fun and powerful!"
vowel_count = 0
for char in sentence.lower():
if char in 'aeiou':
vowel_count += 1
print(f"元音字母出现次数:{vowel_count}")
python
Copy
# 大数据处理模拟
import sys
# 传统列表
data_list = [i**2 for i in range(1000000)]
print(f"列表占用内存:{sys.getsizeof(data_list)} bytes")
# 生成器版本
data_gen = (i**2 for i in range(1000000))
print(f"生成器占用内存:{sys.getsizeof(data_gen)} bytes")
python
Copy
# 素数检测优化算法
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
python
Copy
# 数据清洗示例
raw_data = [34, None, 19, 'missing', 45, 0, -1, 28]
cleaned = []
for item in raw_data:
if not isinstance(item, int):
continue
if item <= 0:
continue
cleaned.append(item)
print(f"有效数据:{cleaned}")
python
Copy
import random
choices = ['石头', '剪刀', '布']
winning_rules = {
('石头', '剪刀'): True,
('剪刀', '布'): True,
('布', '石头'): True
}
while True:
computer = random.choice(choices)
user = input("请出拳(石头/剪刀/布)或输入q退出:")
if user.lower() == 'q':
print("游戏结束")
break
if user not in choices:
print("无效输入,请重新选择!")
continue
print(f"计算机出:{computer}")
if user == computer:
print("平局!")
elif (user, computer) in winning_rules:
print("玩家获胜!")
else:
print("计算机获胜!")
python
Copy
import time
import requests
urls = [
'https://api.example.com/data1',
'https://api.example.com/data2',
'https://api.example.com/data3'
]
retry_limit = 3
delay = 2
for index, url in enumerate(urls, 1):
attempts = 0
while attempts < retry_limit:
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
print(f"成功获取数据源{index}")
break
except Exception as e:
print(f"连接异常:{str(e)}")
attempts += 1
time.sleep(delay)
else:
print(f"数据源{index}获取失败,跳过处理")