python设计一个交互程序实现如下功能

项目描述

python设计一个交互程序,可以实现如下功能:

根据WEO Code查询并显示某个国家某一年的GDP、某些年份的各自的GDP

根据WEO Code查询并显示某个国家GDP均值、某些年份的GDP均值

根据WEO Code查询并显示某个国家某一年的人口数、某些年份的各自的人口数

根据WEO Code查询并显示某个国家人口数均值、某些年份的人口数均值 询并显示全球某一年GDP前10强的国家,以及每年GDP前10强的国家,降序显示

查询并显示全球某一年人口数前10最高的国家,以及每年前10最高的国家,降序显示

案例源码

要实现这个交互程序,你可以使用Python编程语言和相关的库来获取数据并进行处理。以下是一个示例代码,使用了pandas库和requests库来处理数据和发送HTTP请求:

import requests
import pandas as pd

def get_data_from_api(weo_code):
    api_url = f"https://api.worldbank.org/v2/country/{weo_code}/indicator/"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        return data
    else:
        print("Failed to fetch data from API.")

def get_gdp_for_year(data, year):
    gdp_indicator = "NY.GDP.MKTP.CD"  # WEO Code for GDP
    df = pd.DataFrame(data[1])
    df = df[df["indicator"]["id"] == gdp_indicator]
    gdp_value = df[df["date"] == str(year)]["value"].values[0]
    return gdp_value

def get_gdp_average(data, start_year, end_year):
    gdp_indicator = "NY.GDP.MKTP.CD"  # WEO Code for GDP
    df = pd.DataFrame(data[1])
    df = df[df["indicator"]["id"] == gdp_indicator]
    mean_gdp = df[(df["date"] >= str(start_year)) & (df["date"] <= str(end_year))]["value"].mean()
    return mean_gdp

def get_population_for_year(data, year):
    population_indicator = "SP.POP.TOTL"  # WEO Code for population
    df = pd.DataFrame(data[1])
    df = df[df["indicator"]["id"] == population_indicator]
    population_value = df[df["date"] == str(year)]["value"].values[0]
    return population_value

def get_population_average(data, start_year, end_year):
    population_indicator = "SP.POP.TOTL"  # WEO Code for population
    df = pd.DataFrame(data[1])
    df = df[df["indicator"]["id"] == population_indicator]
    mean_population = df[(df["date"] >= str(start_year)) & (df["date"] <= str(end_year))]["value"].mean()
    return mean_population

def get_top_countries_by_gdp(year):
    gdp_indicator = "NY.GDP.MKTP.CD"  # WEO Code for GDP
    api_url = f"https://api.worldbank.org/v2/country/all/indicator/{gdp_indicator}"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        df = pd.DataFrame(data[1])
        df = df[df["date"] == str(year)].dropna(subset=["value"]).sort_values(by="value", ascending=False)
        top_countries = df.head(10)[["countryiso3code", "value"]]
        return top_countries
    else:
        print("Failed to fetch data from API.")

def get_top_countries_by_population(year):
    population_indicator = "SP.POP.TOTL"  # WEO Code for population
    api_url = f"https://api.worldbank.org/v2/country/all/indicator/{population_indicator}"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        df = pd.DataFrame(data[1])
        df = df[df["date"] == str(year)].dropna(subset=["value"]).sort_values(by="value", ascending=False)
        top_countries = df.head(10)[["countryiso3code", "value"]]
        return top_countries
    else:
        print("Failed to fetch data from API.")


# 主程序循环
while True:
    print("请选择要执行的操作:")
    print("1. 查询某个国家某一年的GDP")
    print("2. 查询某个国家GDP的均值")
    print("3. 查询某个国家某一年的人口数")
    print("4. 查询某个国家人口数的均值")
    print("5. 查询全球某一年GDP前10强的国家")
    print("6. 查询全球某一年人口数前10最高的国家")
    print("0. 退出程序")
    choice = input("请输入选项:")

    if choice == "0":
        break

    if choice in ["1", "2", "3", "4"]:
        weo_code = input("请输入WEO Code:")
        data = get_data_from_api(weo_code)

        if choice == "1":
            year = input("请输入年份:")
            gdp = get_gdp_for_year(data, year)
            print(f"{year}年的GDP为:{gdp}")

        elif choice == "2":
            start_year = input("请输入起始年份:")
            end_year = input("请输入结束年份:")
            mean_gdp = get_gdp_average(data, start_year, end_year)
            print(f"{start_year}年到{end_year}年的GDP均值为:{mean_gdp}")

        elif choice == "3":
            year = input("请输入年份:")
            population = get_population_for_year(data, year)
            print(f"{year}年的人口数为:{population}")

        elif choice == "4":
            start_year = input("请输入起始年份:")
            end_year = input("请输入结束年份:")
            mean_population = get_population_average(data, start_year, end_year)
            print(f"{start_year}年到{end_year}年的人口数均值为:{mean_population}")

    elif choice == "5":
        year = input("请输入年份:")
        top_countries = get_top_countries_by_gdp(year)
        print(f"{year}年GDP前10强的国家:")
        print(top_countries)

    elif choice == "6":
        year = input("请输入年份:")
        top_countries = get_top_countries_by_population(year)
        print(f"{year}年人口数前10最高的国家:")
        print(top_countries)

    else:
        print("无效选项,请重新输入。")

在这个程序中,我们使用了世界银行的开放数据API来获取相关的经济和人口数据。根据用户的选择,程序会调用相应的函数来查询并显示所需的数据。

请注意,上述代码仅提供了一个示例框架,你可以根据实际需求进行修改和完善。此外,你可能需要安装pandas和requests库,可以使用pip install pandas requests命令进行安装。

© 版权声明
THE END
喜欢就支持一下吧
点赞9赞赏 分享