程序在输入后停止



我使用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时存在相关错误。

最新更新