json 我怎样才能把这段代码放到一个函数里面呢?

j2qf4p5b  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(133)

2022年10月,我开始攻读数据科学硕士学位。我以前从未编写过代码。我的学术背景是五年前获得经济学学士学位。
Python学科的老师提出了以下问题:
有一个名为http://numbersapi.com的API。这个API是关于 * 数字事实 * 的,你可以在这个API中检查任何一年的事实,即:http://numbersapi.com/1492/year .
在这个例子中,如果你检查这个网址,它会显示“1492年是斐迪南和伊莎贝拉进入格拉纳达1月6日”。
该声明接着说:
构造一个以FY(第一年)和LY(去年)两个年份作为参数的函数。该函数必须收集从FY年到LY年(包括FY年和LY年)的事实,并返回一个字典,其中键是年份,值是关于这一年的事实。
一旦我理解了API,我就编写了以下代码:

import requests

FY = 2015
LY = 2022

a = (f'http://numbersapi.com/{FY}/year')
url_1 = requests.get(a)
print(url_1.text)

while FY < LY:
      b = (f'http://numbersapi.com/{FY+1}/year')
      url_n = requests.get(b)
      print(url_n.text)
      FY += 1
      
      if LY - FY <0:
        print(AI)
      elif LY - FY ==0:
        break

我意识到我前面的代码不在函数内部,也没有字典。
然后,我试着把它放进一个函数中:

import requests

FY = 2015
LY = 2022

def query(url_1, url_n):
    a = (f'http://numbersapi.com/{FY}/year')
    url_1 = requests.get(a)
    print(url_1.text)

    while FY < LY:
          b = (f'http://numbersapi.com/{FY+1}/year')
          url_n = requests.get(b)
          print(url_n.text)
          FY += 1
      
          if LY - FY <0:
            print(FY)
          elif LY - FY ==0:
            break
    return FY, LY

print(url_1, url_n)

一旦我执行了我得到了:
〈回应[200]〉〈回应[200]〉
这就是我自高自大的地方。

icnyk63a

icnyk63a1#

函数的参数应该是FYLY,当你想执行函数时,你必须用一组必需的参数调用函数名。

更新

使用.replace()将year替换为空格,使用.strip()删除周围的所有空格。

def get_year_fact(start_year, end_year):
    output_dict = {}
    url_1 = requests.get(f'http://numbersapi.com/{start_year}/year')
    output_dict[start_year] = url_1.text.replace(f'{start_year} is', '').strip()
    while start_year < end_year:
        url_n = requests.get(f'http://numbersapi.com/{start_year+1}/year')
        output_dict[start_year+1] = url_n.text.replace(f'{start_year+1} is', '').strip()
        start_year += 1
        if end_year - start_year <0:
            print(start_year)
        elif end_year - start_year ==0:
            break
    return output_dict

# call function
output_dict = get_year_fact(FY, LY)
output_dict

但是,您的函数可以更短,如下所示:

def get_year_fact(start_year, end_year):
    output_dict = {}
    for year in range(start_year, end_year+1):
        url = requests.get(f'http://numbersapi.com/{year}/year')
        output_dict[year] = url.text.replace(f'{year} is', '').strip()
    return output_dict

输出:

> output_dict

{2015: 'the year that the Voyager 1 space probe will reach the heliopause.',
 2016: 'the year that the II Winter Youth Olympic Games will be held in Lillehammer, Norway on February - March 6 26th.',
 2017: 'the year that the first possible government in Hong Kong elected by universal suffrage is scheduled to take office on July 1st.',
 2018: 'the year that we do not know what happened.',
 2019: 'the year that an annular solar eclipse will be visible from South Asia on December 26th.',
 2020: 'the year that the main segment of track extending from San Francisco to Anaheim of the California High Speed Rail system is expected to be completed.',
 2021: 'the year that Earliest expected date for the first manned Orion mission.',
 2022: "the year that FIFA's World Cup will be hosted by Qatar."}
ehxuflar

ehxuflar2#

一般来说,函数接受用户的参数作为参数,所以在您的例子中,您希望保持链接不变,并更改作为输入的年份。

import requests

def query(FY, LY):
    a = (f'http://numbersapi.com/{FY}/year')
    url_1 = requests.get(a)
    print(url_1.text)

    while FY < LY:
        b = (f'http://numbersapi.com/{FY+1}/year')
        url_n = requests.get(b)
        print(url_n.text)
        FY += 1
      
        if LY - FY <0:
            print(FY)
        elif LY - FY ==0:
            break
    return FY, LY

query(2015,2022)

相关问题