运行系统:macOS Sonoma 14.6.1
Python编译器:PyCharm 2024.1.4 (Community Edition)
Python版本:3.12
往期链接:
Python项目实战
P151–气象数据爬取
技术栈:数据爬虫
import requests
API_KEY = 'd0d3ed025*******2f14da'
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather'
def get_weather(city):
url = f"{
BASE_URL}?q={
city}&appid={
API_KEY}&units=metric"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data
else:
print(f"无法获取天气数据: {
response.status_code}")
return None
def display_weather(data):
if data:
city = data['name']
temperature = data['main']['temp']
weather_description = data['weather'][0]['description']
humidity = data['main']['humidity']
wind_speed = data['wind']['speed']
print(f"城市: {
city}")
print(f"温度: {
temperature}°C")
print(f"天气: {
weather_description}")
print(f"湿度: {
humidity}%")
print(f"风速: {
wind_speed} m/s")
else:
print("没有可显示的天气数据。")
if __name__ == "__main__":
city = input("请输入城市名称: ")
weather_data = get_weather(city)
display_weather(weather_data)

P152–求解数独问题
技术栈:代码逻辑+回溯法
def is_valid(board, row, col, num):
for x in range(9):
if board[row][x] == num:
return False
for x in range(9):
if board[x][col]