Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active July 13, 2021 15:50
Show Gist options
  • Save qmmr/d00a42c3bfb9c71b9395399dfbe3460a to your computer and use it in GitHub Desktop.
Save qmmr/d00a42c3bfb9c71b9395399dfbe3460a to your computer and use it in GitHub Desktop.
Python Homework Assignment #7: Dictionaries and Sets
"""
pirple.com/python
Python Homework Assignment #7: Dictionaries and Sets
Details:
Return to your first homework assignments, when you described your favorite song.
Refactor that code so all the variables are held as dictionary keys and value.
Then refactor your print statements so that it's a single loop that passes through each item in the dictionary
and prints out it's key and then it's value.
Extra Credit:
Create a function that allows someone to guess the value of any key in the dictionary,
and find out if they were right or wrong.
This function should accept two parameters: Key and Value.
If the key exists in the dictionary and that value is the correct value,
then the function should return true.
In all other cases, it should return false.
"""
# Song attributes dictionary
song = {
"title": "We Are the Champions",
"album": "News of the World",
"artist": "Queen",
"genre": "Arena rock",
"release year": "1977",
"length": "2.59",
"length (seconds)": "179",
"songwriter": "Freddie Mercury",
"producers": "Queen, Mike \"Clay\" Stone"
}
# Print the info aboute song
for key in song:
print("{0:s}: \"{1:s}\"".format(key.capitalize(), song[key]))
def guess(key, val):
return key in song and song[key] == val
print("\n\nBonus:\n\n")
print("Is the title of this song \"We Are the Champions\"?: {}".format(
guess("title", "We Are the Champions")))
print("Is the album name \"News of the World\"?: {}".format(
guess("album", "News of the World")))
@EliansBad
Copy link

def guess(key, val):
if song[key] == val:
return True
else:
return False
this is what i did for it, pretty simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment