Py APP1 (Py.Ud.10Apps) Flashcards
how to load a json file into a dict?
data.json into a1_dict
prerequisite: load json module
]] import json
]] a1_dict = json.load( open( ‘files/data.json’) )
in case of a key error, how to provide a user-friendly error message instead of a traceback?
– use get() to retrieve the value from the dict and an if-else to treat input errors
https://realpython.com/python-keyerror/
ages = {‘Jim’: 30, ‘Pam’: 28, ‘Kevin’: 33}
person = input(‘Get age for: ‘)
age = ages.get(person)
if age:
print( f’{person} is {age} years old.’ )
else:
print( f”{person}’s age is unknown.” )
name a module which is useful in comparing similarities between textstrings
difflib
name two of its modules and describe how they work
sequencematcher( ‘None’, word, string ).ratio( )
– will provide the ratio of similarity, over 0.6 is strong
get_close_matches( ‘word’, [ dictwords to compare against ], , ratio threshold )