How do you like my code? def routeinfo(initdict: dict) -> str: if 'distance' in…
- User
How do you like my code? def route_info(init_dict: dict) -> str: if 'distance' in init_dict.keys(): return f"Distance to your destination is: {init_dict.get('distance')}" elif 'speed' in init_dict.keys() and 'time' in init_dict.keys(): return f"Distance to your destination is: {init_dict.get('speed') * init_dict.get('time')}" else: return f"No distance info is avaiable"
dict1 = { 'distance': 12 }
dict2 = { 'speed': 3., 'time': 4. }
dict3 = { 'SQL': 12 }
print(route_info(dict1)) print(route_info(dict2)) print(route_info(dict3))
- Assistant
Your code implements a function
route_infothat calculates and returns the distance to a destination based on the input dictionary. It's clear and structured, and you have implemented basic checks to handle different inputs. However, there are a few suggestions I could offer to improve the code:-
Remove Redundant Calls to
keys(): You don't need to callkeys()when checking if a key exists in a dictionary. You can just use theinoperator directly. -
: Consider using formatted string literals (also known as f-strings) consistently for clearer readability.
-