Question

Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...

Language: Python

Topic: Dictionaries

Function name: catch_flight
Parameters: dictionary, tuple containing two strings

Returns: dictionary

Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget.
You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and a tuple containing the range of prices (strings) that you are looking for (inclusive). Create a dictionary that has each city name (string) as a key with a value of the number of flight prices (int) that fall within your price range.

>>> cities = {'Atlanta': ['$7.31', '$8.46', '$24.03', '$12.00', '$8.30'], 'Orlando': ['$6.36', '$9.43', '$15.04', '$19.00'], 'Phoenix': ['$1.01', '$3.18', '$4.30', '$18.01']}
>>> prices = ('$8.30', '$17.00')
>>> print(catch_flight(cities, prices))

{'Atlanta': 3, 'Orlando': 2, 'Phoenix': 0}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

catch flight.py - D:/Coding/PYTHON/catch flight.py (3.6.2) File Edit Format Run Options Window Help def catch flight (cities,

OUTPUT :

SS3 /Coding/PYTHON/catch $8.46, RESTART: D: [$7.31, flight.py ーーーーーーーーーーーーーーー >>> cities = { Atlanta: $24.03, $12.00

CODE :

def catch_flight(cities,prices):
    #Convert range to float
    r = (float(prices[0][1:]),float(prices[1][1:]))
    #Initialize dictionary for ans
    ans = {}
    for city in cities:
        if city not in ans:
            ans[city] = 0
        #Compare prices and add to ans
        for price in cities[city]:
            p = float(price[1:])
            if p>=r[0] and p<=r[1]:
                ans[city]+=1
    return ans
                            

Add a comment
Know the answer?
Add Answer to:
Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT