简体   繁体   中英

setting min and max price from API variable in function

I have an API web http://www.boredapi.com/api/activity/ which randomly creates an activity to do if you are bored. The common output is the activity with the followings fields: key,link,participants,accessibility,price and type. I'm only interested in showing the activity, the type and the price.

So far so good.

BUT, I wan to be able to get only the activities that are WITHIN a range of prices. I created the function "fun" and has two parameters (the minimum price and the maximum we are willing to pay.

I want to check in a range from 0 to 10 which activities are available in a specific range of price.

The only thing I can't get around is how to tell the API to print only if the activities are in these ranges... I tried to access the attribute with ['price'] but it doesn't seem to make any difference.

How would you address this issue?

Currently the output is something like this, the only issue is the price range在此处输入图像描述

import requests
import json

def fun(minprice,maxprice):
    for i in range(0,10):
        response= requests.get("http://www.boredapi.com/api/activity/")
        content_dict=json.loads(response.content)
        del(content_dict['key'])
        del(content_dict['link'])
        del(content_dict['participants'])
        del(content_dict['accessibility'])
        minprice=content_dict['price']
        maxprice=content_dict['price']
        print(content_dict)
fun(0,0.1)

Hello from what I understand you are trying to go though each option and check if the price is between two values you can do this by check if the price is between your values like this assuming that content_dict['price'] is a float

if content_dict['price'] > minprice and content_dict['price'] < maxprice:
    # whatever you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM