At the conclusion of this programming assignment, participants should be able to:
if
statementsBefore starting this programming assignment, participants should be able to:
Content used in this assignment is based upon information in the following sources:
Write an "are you a fan" quiz program (quiz.py). The program will quiz the user on a topic of your choosing (keep the topic appropriate!!). For quiz ideas, check out this website.
Note: If you would like to attempt the bonus (see below), you may want to choose a topic that is related to music in some way.
Note: Please cite all sources you use in your code.
Your quiz must contain 10 questions. These 10 questions include at least 2 of each of the following different types of questions:
The user enters in a character "a" through "e" for their answer.
Example:
1) The population of Pullman is roughly what? Please enter a character a-e.
a) 10k
b) 20k
c) 30k
d) 40k
e) 50k
The user enters in a numeric response to an open ended question. I recommend prompting the user to enter an integer. If you choose to use floats, be specific to the user about how they should enter their response (i.e. rounded and/or a certain number of decimal places).
Example:
How many students are enrolled in this class? Please enter an integer.
The user enters 1 or 0 in response to a statement. Convert the string returned by input()
into an integer, and then convert the integer into a Boolean with bool()
. Then compare the bool
response value to the reserved keywords True
or False
.
Example:
Pullman used to be called "Three Forks", true or false? Please enter 1 for True or 0 for False.
int
) if the user answered correctlyint
) if the user answered incorrectlyif-else
structure to do this.main()
function that drives your program. main()
should count the number of correct answers (this is the user's quiz score).if-elif-else
structure, which means you need at least 3 different fun statements based on the user's score).Here is a shortened example run of a quiz based on Game of Thrones. Credit for much of the content goes to these two online quizzes: Q1 and Q2.
Welcome to the Game of Thrones Quiz!!
1) Which Dire-wolf belonged to Robb? Please enter a character a-e.
a) Lady
b) Summer
c) Grey Wind
d) Nimeria
e) Ghost
b
Sorry, that is wrong. The answer is c) Grey Wind.
2) How many dragons does Daenerys own? Please enter an integer.
3
You are correct!
[output removed for brevity]
9) Name one of Daenerys' dragons. Please enter a string in all lower case.
drogon
Awesome, nice work!
10) Who wins the Kingsmoot on the iron isles? Please enter a character a-e.
a) Asha Greyjoy
b) Reek
c) Balon Greyjoy
d) Euron Greyjoy
e) Ramsay Bolton
d
Woohoo, look at you go!
You scored 9 questions correctly. Wow, you know your Game of Thrones!
Identify a (somewhat) popular song that is related to your quiz topic of choice. For example, if your quiz topic is the TV show "Friends", a popular song related to "Friends" is the theme song, "I'll be There for You" by The Rembrandts.
Add an 11th question to your quiz that asks the user, "On a scale from 0 (least popular) to 100 (most popular), what is the current Spotify popularity of <song and song relevance to quiz topic>
?" Answer this question using and if-elif-else
structure, based on how close the user's answer is to the actual Spotfiy popularity.
We can find out the actual, current Spotify popularity for a song by using the Spotify Tracks API. API stands for application programming interface. An API provides functions for programmers to use, without needing to know the details of the implementation. We can request information from Spotify about songs via its API.
At this point in the course, we haven't learned the skills necessary to query the Spotify API and extract the required information from the response. Consequently, I am providing code for you to copy and paste into your program that does this for you. Do not modify this code.
import urllib.request
def format_search_term(search_term):
'''
111 students: no need to call this function
To prepare the search term string for the query:
1. remove comma
2. replace spaces with +
'''
search_term = search_term.replace(",", "")
search_term = search_term.replace(" ", "+")
return search_term
def build_query(query):
'''
111 students: no need to call this function
Builds the query string for the Spotify Search API according to this website:
https://developer.spotify.com/web-api/console/get-search-item/
'''
query_base = "https://api.spotify.com/v1/search?q="
query = query_base + query
# perform a track search and only return the top result
query += "&type=track&limit=1"
return query
def extract_numeric_value(results_str, label):
'''
111 students: no need to call this function
Extracts an integer value represented by the label parameter from the JSON response.
'''
index = results_str.find(label)
results_str = results_str[index:]
index = results_str.find(":")
results_str = results_str[index + 2:]
index = results_str.find(",")
results_str = results_str[:index]
value = int(results_str)
return value
def get_track_popularity(track, artist):
'''
111 STUDENTS: THIS IS THE FUNCTION YOU WILL CALL
Accepts 3 strings representing a track and its artist
Returns the popularity for track and artist
'''
track = format_search_term(track)
artist = format_search_term(artist)
# search the spotify database for a track by artist
search_terms = track + "+" + artist
query = build_query(search_terms)
web_obj = urllib.request.urlopen(query)
# web_obj.read() returns an array of bytes, need to convert to a string
results_str = str(web_obj.read())
web_obj.close()
info = extract_numeric_value(results_str, "popularity")
return info
The only function you need to interact with is get_track_popularity(<track string>, <artist string>)
. Here is an example of how to call get_track_popularity()
:
# I'll be there for you is the theme song from the TV show Friends
popularity = get_track_popularity("I'll be there for you", "The Rembrandts")
print("I'll be there for you by The Rembrandts has popularity %d." %(popularity))
I'll be there for you by The Rembrandts has popularity 57.
<your last name>_pa3.zip
by the due date and time.Note: By submitting your code to be graded, you are stating that your submission does not violate the CptS 111 Academic Integrity Policy outlined in the Syllabus.
This assignment is worth 100 points + 5 points bonus. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria:
bool
typereturn
values (integers 1 or 0) to denote correct/incorrect answersmain()
functionif-elif-else
structure