From 3c77953d07a2ca6ae4eafc072ecc48fac89b62bc Mon Sep 17 00:00:00 2001 From: Anne Gentle Date: Tue, 7 Aug 2018 16:49:00 -0500 Subject: [PATCH] Adds Solutions branch with modified Python files with solutions to hands-on exercises --- intro-python/parsing-json/nested_data.py | 8 +++++++- intro-python/part1/hands_on_exercise.py | 25 +++++++++++++++++++----- intro-python/part2/fortune_cookie.py | 8 +++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/intro-python/parsing-json/nested_data.py b/intro-python/parsing-json/nested_data.py index 306d899..a389c26 100644 --- a/intro-python/parsing-json/nested_data.py +++ b/intro-python/parsing-json/nested_data.py @@ -12,7 +12,13 @@ with open(os.path.join(here, "interfaces.json")) as file: # TODO: Parse the contents of the JSON file into a variable - + json_data = json.loads(file.read()) # TODO: Loop through the interfaces in the JSON data and print out each # interface's name, ip, and netmask. +for interface in json_data["ietf-interfaces:interfaces"]["interface"]: + print("{name}: {ip} {netmask}".format( + name=interface["name"], + ip=interface["ietf-ip:ipv4"]["address"][0]["ip"], + netmask=interface["ietf-ip:ipv4"]["address"][0]["netmask"], + )) diff --git a/intro-python/part1/hands_on_exercise.py b/intro-python/part1/hands_on_exercise.py index 5ea24c3..52c124a 100644 --- a/intro-python/part1/hands_on_exercise.py +++ b/intro-python/part1/hands_on_exercise.py @@ -7,24 +7,39 @@ # TODO: Write a print statement that displays both the type and value of `pi` pi = math.pi +print("pi is a {} with a value of {}".format(type(pi), pi)) # TODO: Write a conditional to print out if `i` is less than or greater than 50 i = random.randint(0, 100) +if i < 50: + print("i is less than 50") +elif i == 50: + print("i is equal to 50") +else: + print("i is greater than 50") # TODO: Write a conditional that prints the color of the picked fruit picked_fruit = random.choice(['orange', 'strawberry', 'banana']) +if picked_fruit == 'orange': + print("The fruit is orange") +elif picked_fruit == 'strawberry': + print("The fruit is red") +elif picked_fruit == 'banana': + print("The fruit is yellow") # TODO: Write a function that multiplies two numbers and returns the result -# Define the function here. +def multiply(num1, num2): + """Multiply two numbers and return the result.""" + result = num1 * num2 + return result # TODO: Now call the function a few times to calculate the following answers +print("12 x 96 =", multiply(12, 96)) -print("12 x 96 =",) +print("48 x 17 =", multiply(48, 17)) -print("48 x 17 =",) - -print("196523 x 87323 =",) +print("196523 x 87323 =", multiply(196523, 87323)) diff --git a/intro-python/part2/fortune_cookie.py b/intro-python/part2/fortune_cookie.py index d7ea949..7760858 100755 --- a/intro-python/part2/fortune_cookie.py +++ b/intro-python/part2/fortune_cookie.py @@ -35,7 +35,13 @@ def create_fortune_cookie_message(how_many_lucky_numbers: int) -> str: # generate_lucky_numbers() and then composing and returning the fortune # cookie's message. - raise NotImplementedError() + fortune = generate_fortune() + lucky_numbers = generate_lucky_numbers(how_many_lucky_numbers) + + return "{fortune}\nLucky Numbers: {lucky_numbers}".format( + fortune = fortune, + lucky_numbers = lucky_numbers, + ) def main():