python - 输入后程序停止
问题描述
我正在使用 openweather api 来获取当前天气。我可以让它使用此代码显示天气数据。(格式化的api代码出来)
import requests
def current_weather():
city_name = ('Houston')
api_key = ('My api code')
url = ('http://api.openweathermap.org/data/2.5/weather?q={}&appid={}').format(city_name, api_key)
info = requests.get(url).json()
print(info);
current_weather()
结果:
{
'coord': {
'lon': -95.3633,
'lat': 29.7633
},
'weather': [
{
'id': 800,
'main': 'Clear',
'description': 'clear sky',
'icon': '01d'
}
],
'base': 'stations',
'main': {
'temp': 295.42,
'feels_like': 294.72,
'temp_min': 294.14,
'temp_max': 297.09,
'pressure': 1024,
'humidity': 39
},
'visibility': 10000,
'wind': {
'speed': 2.24,
'deg': 66,
'gust': 3.58
},
'clouds': {
'all': 1
},
'dt': 1634495143,
'sys': {
'type': 2,
'id': 2006306,
'country': 'US',
'sunrise': 1634473467,
'sunset': 1634514521
},
'timezone': -18000,
'id': 4699066,
'name': 'Houston',
'cod': 200
}
[Finished in 287ms]
但是对于城市名称,我尝试在输入中获取它,它只是询问我在哪个城市并停止程序。
def current_weather():
city_name = input('What city are you in?: ')
api_key = ('My api code')
url = ('http://api.openweathermap.org/data/2.5/weather?q={}&appid={}').format(city_name, api_key)
info = requests.get(url).json()
print(info);
current_weather()
结果:你在哪个城市?:巴黎
没有别的了。
解决方案
我自己尝试了代码,它似乎应该可以工作。print(info)
在其他语言之后返回 python 时,您似乎有一个分号,相关错误。
推荐阅读
- python - 我应该能够使用 gspread 和 Python 来使用公式/函数更新 Google 表格中的单元格吗?
- javascript - 我的地图函数不返回任何信息?
- python - 根据值是否为空来创建熊猫数据框列
- excel - 如何使用值/格式将列从一个工作表复制到新工作表中的另一个空列
- python - 为什么使用独立 bzip2 和 pandas to_csv(, compression='bz2') 函数之间的文件大小存在差异?
- swift - Firebase 安全问题
- javascript - 如何在聊天窗口中提交表单后显示另一个 div
- .net - 在 net48 中引用 netstandard ServiceStact.redis
- android - 是否可以在 React Native 中同时构建多个应用程序?
- wordpress - 代码片段破坏了 wordpress 图片上传功能,不确定这两个代码片段我做错了什么?