In: Computer Science
Python
create a function tryhard and print out the dictionary as a string form.
For example
def tryhard(d:dict):
#Code here
input: d = {'first': {}, 'second': {'1': {'move'}, '0': {'move', 'slow'}}, 'third': {'1': {'stop'}}}
output = " first movie: [ ]\n third movie: [('1', ['stop'])]\n second movie: [('0', ['slow', 'move']), ('1', ['move'])]\n"
Please look at my code and in case of indentation issues check the screenshots.
------------main.py----------------
def tryhard(d):
output = ""
#create an empty string for
output
for key, val in d.items():
output += key + " movie:
" #Add key name and " movie": to
output
tupleList = []
#create a tupleList for each movie
if len(key) > 0:
#check if
movie has values
for key2, val2
in d[key].items(): #loop through nested
dictionary
tup = (key2, list(val2))
#form a tuple
tupleList.append(tup)
#append a tuple to the
list
output += str(tupleList) + "\n"
# string of tupleList to
output
print(output)
d = {'first': {}, 'second': {'1': {'move'}, '0': {'move',
'slow'}}, 'third': {'1': {'stop'}}}
tryhard(d)
--------------Screenshots-------------------
------------Output-----------------
---------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou