Question

Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating...

Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating point numbers. The function will make an HTTPS GET request to the url "https://fury.cse.buffalo.edu/ps-api/a" with a query string containing the same key-value pairs from the input key-value store. The response from the server will be a JSON string representing an object in the format "{"answer": <Number>}" where <Number> is a floating point Number. Return the value at the key "answer" as a float

Recall that you may need to convert the numbers to strings

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

Please let me know if you need more information:-

==========================================

import urllib.request

import json

def query_dict(mapping_strings):

url_to_send_request=r'https://fury.cse.buffalo.edu/ps-api/a'

#framing query to send with mapping strings - START

i=0

for pair in mapping_strings:

if i == 0:

url_to_send_request += "?";

else:

url_to_send_request += "&"

url_to_send_request += pair + "=" + str(mapping_strings[pair])

i=i+1

#framing query to send with mapping strings - END

#url_to_send_request=url_to_send_request+'?'+urllib.parse.urlencode(mapping_strings) #WAY-2 with urllib

print(url_to_send_request)

response_from_url=urllib.request.urlopen(url_to_send_request).read().decode()

json_response_from_url=json.loads(response_from_url)

return float(json_response_from_url['answer'])

mapping_strings={'x':15,'y':14,'z':25}

print("Floating noint number is:",query_dict(mapping_strings))



==

import urllib.request import json def query_dict(mapping_strings) : url_to_send_request-rhttps://fury.cse.buffalo.edu/ps-api

==

==

OUTPUT:-

====

https://fury.cse.buffalo.edu/ps-api/a?x=15&y=14&z=25
Floating noint number is: 3976.19

==

Please let me know if you need more information:-

==

Thanks

Add a comment
Know the answer?
Add Answer to:
Write a function named "query_dict" that a key-value store as a parameter mapping strings to floating...
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