In: Computer Science
Q6. The question does not include 202010mid.txt. Have made assumptions about the file and written the code; if any doubts, please ask in comments:
tuplst = []
with open ("202010mid.txt", "r") as f:
while True:
s = f.readline()
if s == '':
break
pair = s.split()
t = (pair[0], pair[1])
tuplst.append (t)
print (tuplst)
//////// Here is the output ////////////////////////////////////////
[('Arnold', '85'), ('Schwarz', '62'), ('Peter', '70'), ('Nick', '90'), ('Don', '99'), ('Trump', '40')]
//////// This is for an input file that looks like this /////////////////
Arnold 85
Schwarz 62
Peter 70
Nick 90
Don 99
Trump 40
Q7. part 1:
import requests
x = requests.get ("https://edition.cnn.com/news.html")
if (x.status_code != 200):
print ("Page not found.")
Running the above shows 'Page not found.' since the request returns a 404 error.
Q7. part 2:
import requests
url = "http://api.open-notify.org/iss-pass.json"
par = {'lat' : 40.71, 'lon' : -74}
r = requests.get (url, par)
print (r.json())
## Below are the results
{'message': 'success', 'request': {'altitude': 100, 'datetime': 1604213248, 'latitude': 40.71, 'longitude': -74.0, 'passes': 5}, 'response': [{'duration': 653, 'risetime': 1604214313}, {'duration': 596, 'risetime': 1604220175}, {'duration': 560, 'risetime': 1604226062}, {'duration': 622, 'risetime': 1604231891}, {'duration': 645, 'risetime': 1604237696}]}