import random
weather_data = {
"北京": {"temp": 5, "weather": "晴", "humidity": 30},
"上海": {"temp": 12, "weather": "多云", "humidity": 50},
"广州": {"temp": 20, "weather": "小雨", "humidity": 70},
"深圳": {"temp": 22, "weather": "阴", "humidity": 65},
"香港": {"temp": 25, "weather": "晴", "humidity": 60},
}
def get_weather(city):
city = city.title()
if city in weather_data:
data = weather_data[city]
temp = data['temp'] + random.randint(-2, 2)
humidity = data['humidity'] + random.randint(-5, 5)
weather = data['weather']
print(f"📍 城市:{city}")
print(f"🌡️ 温度:{temp}°C")
print(f"🌥️ 天气:{weather}")
print(f"💧 湿度:{humidity}%")
else:
print("❌ 没有找到这个城市哦~")
city = input("请输入城市名称:")
get_weather(city)
import random
weather_data = {
"北京": {"temp": 5, "weather": "晴", "humidity": 30},
"上海": {"temp": 12, "weather": "多云", "humidity": 50},
"广州": {"temp": 20, "weather": "小雨", "humidity": 70},
"深圳": {"temp": 22, "weather": "阴", "humidity": 65},
"香港": {"temp": 25, "weather": "晴", "humidity": 60},
}
weather_emoji = {
"晴": "☀️",
"多云": "🌥️",
"阴": "☁️",
"小雨": "🌧️",
"雷阵雨": "⛈️"
}
def humidity_level(h):
if h < 40:
return "干燥 🏜️"
elif 40 <= h <= 60:
return "舒适 🌿"
else:
return "潮湿 🌧️"
def get_weather(city):
city = city.strip().title()
if city in weather_data:
data = weather_data[city]
temps = [data['temp'] + random.randint(-2, 2) for _ in range(3)]
humidity = data['humidity'] + random.randint(-5, 5)
weather = data['weather']
emoji = weather_emoji.get(weather, "")
print(f"\n📍 城市:{city}")
print(f"🌥️ 天气:{weather} {emoji}")
print(f"💧 湿度:{humidity}% ({humidity_level(humidity)})")
print("🌡️ 未来三天天气预测:")
days = ["今天", "明天", "后天"]
for i in range(3):
print(f" {days[i]}:{temps[i]}°C {weather} {emoji}")
else:
print("❌ 没有找到这个城市哦~")
print("可查询城市列表:", ", ".join(weather_data.keys()))
print("欢迎使用离线天气查询小工具!输入“退出”即可结束查询。")
while True:
city = input("\n请输入城市名称:")
if city.strip() == "退出":
print("💖 感谢使用,再见~")
break
get_weather(city)