Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save UcheAnyiam/b9de5208957e07157712381413ab7170 to your computer and use it in GitHub Desktop.

Select an option

Save UcheAnyiam/b9de5208957e07157712381413ab7170 to your computer and use it in GitHub Desktop.
Homework Assignment #7: Dictionaries and Sets Details: Return to your first homework assignments, when you described your favourite 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…
"""
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.
"""
#My dictionary
variables = {"artist":"Teyana Taylor", "genre":"R&B", "length":"3.47", "theme":"80s vibes", "colours":"sepia, vignette"}
#to print all the keys in my dictionary
for key in variables.keys():
print (key, end=", ")
#The extra credit section, which also lists only the keys in the dictionary
def listkeys():
print("Welcome to...'Guess the values of the keys!")
print("The keys will be listed below, and all you need to do is guess the right value")
import time
time.sleep(3)
return [*variables]
#The extra credit section. This prompts for an input of the key and value in the dictionary, and depending on the input, it reveals whether correct or wrong
def guess_the_keys_val():
key = input("Enter a Key\n")
if key in variables.keys():
import time
time.sleep(2)
print("Nice one\n")
value = input("Enter a Value\n")
print("*drumroll.....*")
if value in variables[key]:
import time
time.sleep(2)
return "Great Job, you're RIGHT!"
else:
import time
time.sleep(2)
return "Oops so close, but yet so far"
else:
return "WRONG!, the road ends here"
#Calling both functions
print(listkeys())
print(guess_the_keys_val())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment