From f2ffc6550e84ab82e891c77591312a1024b0704b Mon Sep 17 00:00:00 2001 From: ybrenning Date: Thu, 15 Sep 2022 17:17:56 +0200 Subject: [PATCH] Fix typos and add comments to ch09 --- .../1-tuples-are-immutable-sequences.py | 2 +- .../4-challenge-list-of-lists.py | 2 +- .../7-challenge-capital-city-loop.py | 3 +++ .../9a-challenge-cats-with-hats.py | 14 +++++++++++++- .../9c-challenge-cats-with-hats.py | 7 +++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ch09-lists-tuples-and-dictionaries/1-tuples-are-immutable-sequences.py b/ch09-lists-tuples-and-dictionaries/1-tuples-are-immutable-sequences.py index 96d6ad8..6f0f1ff 100644 --- a/ch09-lists-tuples-and-dictionaries/1-tuples-are-immutable-sequences.py +++ b/ch09-lists-tuples-and-dictionaries/1-tuples-are-immutable-sequences.py @@ -13,7 +13,7 @@ # Exercise 3 -# unpack the tuple into three string and display them +# Unpack the tuple into three strings and display them position1, position2, position3 = cardinal_numbers print(position1) print(position2) diff --git a/ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py b/ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py index ccccb6c..f723245 100644 --- a/ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py +++ b/ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py @@ -24,7 +24,7 @@ def mean(values): def median(values): """Return the median value of the list `values`""" values.sort() - # If the number of valus is odd, + # If the number of values is odd, # return the middle value of the list if len(values) % 2 == 1: # The value at the center of the list is the value diff --git a/ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py b/ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py index d662974..8eca70b 100644 --- a/ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py +++ b/ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py @@ -56,8 +56,11 @@ "Wyoming": "Cheyenne", } +# Pull random state and capital pair from the dict by casting to list of tuples state, capital = random.choice(list(capitals_dict.items())) +# Game loop continues until the user inputs "exit" +# or guesses the correct capital while True: guess = input(f"What is the capital of '{state}'? ").lower() if guess == "exit": diff --git a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py index 3e081c0..74cb1ed 100644 --- a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py +++ b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py @@ -4,18 +4,30 @@ def get_cats_with_hats(array_of_cats): cats_with_hats_on = [] + # We want to walk around the circle 100 times for num in range(1, 100 + 1): + # Each time we walk around, we visit 100 cats for cat in range(1, 100 + 1): + # Determine whether to visit the cat + # Use modulo operator to visit every 2nd, 3rd, 4th,... etc. if cat % num == 0: + # Remove or add hat depending on + # whether the cat already has one if array_of_cats[cat] is True: array_of_cats[cat] = False else: array_of_cats[cat] = True + + # Add all number of each cat with a hat to list for cat in range(1, 100 + 1): - if cats[cat] is True: + if array_of_cats[cat] is True: cats_with_hats_on.append(cat) + + # Return the resulting list return cats_with_hats_on +# Cats contains whether each cat already has a hat on, +# by default all are set to false since none have been visited cats = [False] * (100 + 1) print(get_cats_with_hats(cats)) diff --git a/ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py b/ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py index c154a31..1e4c885 100644 --- a/ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py +++ b/ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py @@ -4,17 +4,24 @@ theCats = {} +# By default, no cats have been visited +# so we set every cat's number to False for i in range(1, 101): theCats[i] = False +# Walk around the circle 100 times for i in range(1, 101): + # Visit all cats each time we do a lap for cats, hats in theCats.items(): + # Determine whether or not we visit a cat if cats % i == 0: + # Add or remove the hat if theCats[cats]: theCats[cats] = False else: theCats[cats] = True +# Print whether each cat has a hat for cats, hats in theCats.items(): if theCats[cats]: print(f"Cat {cats} has a hat.") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy