Python API example
Ethan Zhao
import requests
import csv
testing = 1
if testing == 0:
url = "https://visual-crossing-weather.p.rapidapi.com/forecast"
querystring = {"aggregateHours":"24","location":"Washington,DC,USA","contentType":"csv","unitGroup":"us","shortColumnNames":"0"}
headers = {
"X-RapidAPI-Key": "eb0bbc6cc0msh085b254f1761bc2p154cf4jsne59a8dbdbaa0",
"X-RapidAPI-Host": "visual-crossing-weather.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
f = open("./assets/weather.csv", "a")
f.write("")
f.write(response.text)
f.close()
with open('./assets/weather.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'{row[0]} {row[9]} {row[1]}')
line_count += 1
else:
location = row[0].replace(",", " " )
print(f'{location} is projected to be {row[9]} degrees on {row[1]} degrees.')
line_count += 1
Code explanation
I import the getpass library that way I can personalize it and say the user name.
The function question_with_response
asks the question and returns the user answer, named msg
.
For every question in the list questions
, I pass the variable msg
into rsp
and send it to the gradeResponse
function, along with the corresponding answer.
gradeResponse
will then check if the answer is correct and award a point to the user if it is.
At the end of the quiz, it will display how many out of the total questions were correct.