如何将用户输入添加到字典

时间:2019-07-31 15:27:11

标签: python python-3.x dictionary

我正在尝试从天气网站的API构建图表。我想让用户输入我想知道当前温度的3个城市,然后从api和用户输入{name of the city - user input: the currently temp from the api}制作字典,然后将字典制作成图{name of the city: the currently temp from the api}

我使用的api是drksky api 我主要使用这个:

import matplotlib.pyplot as plt for the graph
import forecastio
import matplotlib.pyplot as plt



def read_key_from_file():
    hFile = open(file_name,'r')
    for line in hFile:
        Mykey = line
    hFile.close()
    Mykey = line
    return  Mykey

def get_geo_location(location):
    location = Nominatim().geocode(location)
    lat = location.latitude
    lon = location.longitude
    return location, lat, lon


def comperison_graph(Mykey): 

    if daily_temp_dict == {}:
        a = input("Enter first city name in lower case letter: ")
        daily_temp_dict[a] = ""
        b = input("Enter second city name in lower case letter: ")
        daily_temp_dict[b] = ""
        c = input("Enter third city name in lower case letter: ")
        daily_temp_dict[c] = ""
        for city in daily_temp_dict.keys():
                location, lat, lon = get_geo_location(city)
                forecast = forecastio.load_forecast(Mykey, lat, lon)
                daily_temp_dict[city] = forecast.currently().temperature
                data =  daily_temp_dict
                names = list(data.keys())
                print(names)
                values = list(data.values())
                print(values)
                plt.bar(0,values[0],tick_label=names[0])
                plt.bar(1,values[1],tick_label=names[1])
                plt.bar(2,values[2],tick_label=names[2])
                plt.xticks(range(0,3),names)
                plt.savefig('fruit.png')
                plt.show()

问题在于,即时通讯会在结果中显示如下图:

https://imgur.com/raVSVxH

我只需要3个城市中的最后一个

不幸的是我做不到

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

您应该使用一个空字符串""作为值创建一个新密钥:

if cities_for_txt_file == {}:
    a = input("Enter first city name in lower case letter: ")
    cities_for_txt_file[a] = ""
    b = input("Enter second city name in lower case letter: ")
    cities_for_txt_file[b] = ""
    c = input("Enter third city name in lower case letter: ")
    cities_for_txt_file[c] = ""
    for city in cities_for_txt_file:
            location, lat, lon = get_geo_location(city)
            forecast = forecastio.load_forecast(Mykey, lat, lon)

然后将forecast作为值分配给城市:

            cities_for_txt_file[city] = forecast

注意:由于您没有提供完整的代码,因此很有可能无法处理错误,例如get_geo_location()中找不到的城市。我建议您在代码中添加一些“尝试/捕获”。

编辑:当您编辑问题时,这是我对新问题的回答。

您不应在循环中进行所有绘制工作,而应在循环之后进行。它应该看起来像这样:

    for city in daily_temp_dict.keys():
        location, lat, lon = get_geo_location(city)
        forecast = forecastio.load_forecast(Mykey, lat, lon)
        daily_temp_dict[city] = forecast.currently().temperature
    data =  daily_temp_dict
    names = list(data.keys())
    print(names)
    values = list(data.values())
    print(values)
    plt.bar(0,values[0],tick_label=names[0])
    plt.bar(1,values[1],tick_label=names[1])
    plt.bar(2,values[2],tick_label=names[2])
    plt.xticks(range(0,3),names)
    plt.savefig('fruit.png')
    plt.show()

答案 1 :(得分:0)

字典不能那样工作,字典不支持append()

我认为您不知道字典是如何工作的,建议您阅读一点

https://www.tutorialspoint.com/python3/python_dictionary

https://www.programiz.com/python-programming/dictionary