首页 > 解决方案 > 输入后程序停止

问题描述

我正在使用 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()

结果:你在哪个城市?:巴黎

没有别的了。

标签: pythonapiinputopenweathermap

解决方案


我自己尝试了代码,它似乎应该可以工作。print(info)在其他语言之后返回 python 时,您似乎有一个分号,相关错误。


推荐阅读