From f2ffc6550e84ab82e891c77591312a1024b0704b Mon Sep 17 00:00:00 2001 From: ybrenning Date: Thu, 15 Sep 2022 17:17:56 +0200 Subject: [PATCH 1/7] 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.") From 5889cf4cd2dd69c9fefa89db4e57d48e6f5b72f3 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Fri, 30 Sep 2022 11:05:18 -0700 Subject: [PATCH 2/7] Use Path.cwd() instead of Path.home() --- ...-challenge-move-all-image-files-to-a-new-directory.py | 8 +------- .../7-challenge-create-a-high-scores-list.py | 9 +++------ .../1-extract-text-from-a-pdf.py | 6 +++--- .../4-concatenating-and-merging-pdfs.py | 2 +- .../5-rotating-and-cropping-pdf-pages.py | 2 +- .../6-encrypting-and-decrypting-pdfs.py | 2 +- .../7-challenge-unscramble-a-pdf.py | 2 +- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/ch12-file-input-and-output/4-challenge-move-all-image-files-to-a-new-directory.py b/ch12-file-input-and-output/4-challenge-move-all-image-files-to-a-new-directory.py index 5affd34..b39266c 100644 --- a/ch12-file-input-and-output/4-challenge-move-all-image-files-to-a-new-directory.py +++ b/ch12-file-input-and-output/4-challenge-move-all-image-files-to-a-new-directory.py @@ -4,13 +4,7 @@ from pathlib import Path # Change this path to match the location on your computer -documents_dir = ( - Path.home() / - "python-basics-exercises" / - "ch11-file-input-and-output" / - "practice_files" / - "documents" -) +documents_dir = Path.cwd() / "practice_files" / "documents" # Create an images/ directory in your home directory images_dir = Path.home() / "images" diff --git a/ch12-file-input-and-output/7-challenge-create-a-high-scores-list.py b/ch12-file-input-and-output/7-challenge-create-a-high-scores-list.py index 3d0f399..f9539f8 100644 --- a/ch12-file-input-and-output/7-challenge-create-a-high-scores-list.py +++ b/ch12-file-input-and-output/7-challenge-create-a-high-scores-list.py @@ -6,12 +6,9 @@ # Change the path below to match the location on your computer scores_csv_path = ( - Path.home() - / "github/realpython" - / "python-basics-exercises" - / "ch12-file-input-and-output" + Path.cwd() / "practice_files" - "scores.csv" + / "scores.csv" ) with scores_csv_path.open(mode="r", encoding="utf-8") as file: @@ -35,7 +32,7 @@ # The high_scores dictionary now contains one key for each name that was # in the scores.csv file, and each value is that player's highest score. -output_csv_file = Path.home() / "high_scores.csv" +output_csv_file = Path.cwd() / "high_scores.csv" with output_csv_file.open(mode="w", encoding="utf-8") as file: writer = csv.DictWriter(file, fieldnames=["name", "high_score"]) writer.writeheader() diff --git a/ch14-interact-with-pdf-files/1-extract-text-from-a-pdf.py b/ch14-interact-with-pdf-files/1-extract-text-from-a-pdf.py index 857ac36..49e1d76 100644 --- a/ch14-interact-with-pdf-files/1-extract-text-from-a-pdf.py +++ b/ch14-interact-with-pdf-files/1-extract-text-from-a-pdf.py @@ -15,10 +15,10 @@ from PyPDF2 import PdfFileReader # To create a PdfFileReader instance, you need to path to the PDF file. -# We'll assume you downloaded the solutions folder and extracted it into -# the home directory on your computer. If this is not the case, you'll +# We'll assume you downloaded the solutions folder and are running this +# program from the solutions folder. If this is not the case, you'll # need to update the path below. -pdf_path = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/zen.pdf" +pdf_path = Path.cwd() / "practice_files" / "zen.pdf" # Now you can create the PdfFileReader instance. Remember that # PdfFileReader objects can only be instantiated with path strings, not diff --git a/ch14-interact-with-pdf-files/4-concatenating-and-merging-pdfs.py b/ch14-interact-with-pdf-files/4-concatenating-and-merging-pdfs.py index d471a17..0753bf6 100644 --- a/ch14-interact-with-pdf-files/4-concatenating-and-merging-pdfs.py +++ b/ch14-interact-with-pdf-files/4-concatenating-and-merging-pdfs.py @@ -15,7 +15,7 @@ from PyPDF2 import PdfFileMerger -BASE_PATH = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files" +BASE_PATH = Path.cwd() / "practice_files" pdf_paths = [BASE_PATH / "merge1.pdf", BASE_PATH / "merge2.pdf"] pdf_merger = PdfFileMerger() diff --git a/ch14-interact-with-pdf-files/5-rotating-and-cropping-pdf-pages.py b/ch14-interact-with-pdf-files/5-rotating-and-cropping-pdf-pages.py index 4bb60b5..3abe217 100644 --- a/ch14-interact-with-pdf-files/5-rotating-and-cropping-pdf-pages.py +++ b/ch14-interact-with-pdf-files/5-rotating-and-cropping-pdf-pages.py @@ -15,7 +15,7 @@ from PyPDF2 import PdfFileReader, PdfFileWriter -pdf_path = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/split_and_rotate.pdf" +pdf_path = Path.cwd() / "practice_files" / "split_and_rotate.pdf" pdf_reader = PdfFileReader(str(pdf_path)) pdf_writer = PdfFileWriter() diff --git a/ch14-interact-with-pdf-files/6-encrypting-and-decrypting-pdfs.py b/ch14-interact-with-pdf-files/6-encrypting-and-decrypting-pdfs.py index 9db418a..22a365a 100644 --- a/ch14-interact-with-pdf-files/6-encrypting-and-decrypting-pdfs.py +++ b/ch14-interact-with-pdf-files/6-encrypting-and-decrypting-pdfs.py @@ -15,7 +15,7 @@ from PyPDF2 import PdfFileReader, PdfFileWriter -pdf_path = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/top_secret.pdf" +pdf_path = Path.cwd() / "practice_files" / "top_secret.pdf" pdf_reader = PdfFileReader(str(pdf_path)) pdf_writer = PdfFileWriter() diff --git a/ch14-interact-with-pdf-files/7-challenge-unscramble-a-pdf.py b/ch14-interact-with-pdf-files/7-challenge-unscramble-a-pdf.py index d66e6a8..ded54b2 100644 --- a/ch14-interact-with-pdf-files/7-challenge-unscramble-a-pdf.py +++ b/ch14-interact-with-pdf-files/7-challenge-unscramble-a-pdf.py @@ -10,7 +10,7 @@ def get_page_text(page): return page.extractText() -pdf_path = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/scrambled.pdf" +pdf_path = Path.cwd() / "practice_files" / "scrambled.pdf" pdf_reader = PdfFileReader(str(pdf_path)) pdf_writer = PdfFileWriter() From 921dc7d612c07d3358858a0820471da0bfe30225 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Wed, 2 Aug 2023 12:58:04 +0200 Subject: [PATCH 3/7] Fix typos in comments --- ch08-conditional-logic/6-recover-from-errors.py | 2 +- .../8b-challenge-simulate-a-coin-toss-experiment.py | 2 +- .../8c-challenge-simulate-a-coin-toss-experiment.py | 4 ++-- ch08-conditional-logic/9a-challenge-simulate-an-election.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ch08-conditional-logic/6-recover-from-errors.py b/ch08-conditional-logic/6-recover-from-errors.py index 157536d..250144b 100644 --- a/ch08-conditional-logic/6-recover-from-errors.py +++ b/ch08-conditional-logic/6-recover-from-errors.py @@ -15,7 +15,7 @@ # Exercise 2 -# Print character and specifid index in string +# Print character and specified index in string input_string = input("Enter a string: ") diff --git a/ch08-conditional-logic/8b-challenge-simulate-a-coin-toss-experiment.py b/ch08-conditional-logic/8b-challenge-simulate-a-coin-toss-experiment.py index a01a8ce..9f3effe 100644 --- a/ch08-conditional-logic/8b-challenge-simulate-a-coin-toss-experiment.py +++ b/ch08-conditional-logic/8b-challenge-simulate-a-coin-toss-experiment.py @@ -31,7 +31,7 @@ def coin_flip(): first_flip = coin_flip() flips = flips + 1 # Continue flipping the coin and updating the tally until - # a different result is returned by coin_flips() + # a different result is returned by coin_flip() while coin_flip() == first_flip: flips = flips + 1 # Increment the flip tally once more to account for the diff --git a/ch08-conditional-logic/8c-challenge-simulate-a-coin-toss-experiment.py b/ch08-conditional-logic/8c-challenge-simulate-a-coin-toss-experiment.py index 5d1e075..753e91d 100644 --- a/ch08-conditional-logic/8c-challenge-simulate-a-coin-toss-experiment.py +++ b/ch08-conditional-logic/8c-challenge-simulate-a-coin-toss-experiment.py @@ -16,9 +16,9 @@ def single_trial(): - """Simulate repeatedly a coing until both heads and tails are seen.""" + """Simulate repeatedly flipping a coin until both heads and tails are seen.""" # This function uses random.randint() to simulate a single coin toss. - # randing(0, 1) randomly returns 0 or 1 with equal probability. We can + # randint(0, 1) randomly returns 0 or 1 with equal probability. We can # use 0 to represent heads and 1 to represent tails. # Flip the coin the first time diff --git a/ch08-conditional-logic/9a-challenge-simulate-an-election.py b/ch08-conditional-logic/9a-challenge-simulate-an-election.py index 2b46837..a9ef523 100644 --- a/ch08-conditional-logic/9a-challenge-simulate-an-election.py +++ b/ch08-conditional-logic/9a-challenge-simulate-an-election.py @@ -26,7 +26,7 @@ else: votes_for_B = votes_for_B + 1 - # Determine who wins the erd region + # Determine who wins the 3rd region if random() < 0.17: votes_for_A = votes_for_A + 1 else: From d65ea99c4bfac477e0d94c3450a2ba02480bc8be Mon Sep 17 00:00:00 2001 From: martin-martin Date: Wed, 2 Aug 2023 13:03:08 +0200 Subject: [PATCH 4/7] One more typo --- .../7-simulate-events-and-calculate-probabilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py b/ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py index 29de41f..d7b939f 100644 --- a/ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py +++ b/ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py @@ -6,7 +6,7 @@ # Exercise 1 -# Write a function that simulatee the roll of a die. +# Write a function that simulates the roll of a die. def roll(): """Return random integer between 1 and 6""" return randint(1, 6) From c4edec716060fe93a465c3955f61628ae716308e Mon Sep 17 00:00:00 2001 From: Martin Breuss Date: Sun, 15 Oct 2023 13:37:50 +0200 Subject: [PATCH 5/7] Fix technical typo --- .../3-manipulate-strings-with-methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch04-strings-and-string-methods/3-manipulate-strings-with-methods.py b/ch04-strings-and-string-methods/3-manipulate-strings-with-methods.py index a37fed6..79b6279 100644 --- a/ch04-strings-and-string-methods/3-manipulate-strings-with-methods.py +++ b/ch04-strings-and-string-methods/3-manipulate-strings-with-methods.py @@ -27,7 +27,7 @@ string3 = " Cheeseburger " print(string1.strip()) # Could also use .lstrip() -print(string1.strip()) # Could also use .rstrip() +print(string2.strip()) # Could also use .rstrip() print(string3.strip()) From d97fbe0ccdd17ea3e10271cd2da05065d255a0af Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Tue, 7 Nov 2023 09:49:42 +0100 Subject: [PATCH 6/7] Expand ch10-3 solution --- ch10-primer-on-oop/3-inherit-from-other-classes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ch10-primer-on-oop/3-inherit-from-other-classes.py b/ch10-primer-on-oop/3-inherit-from-other-classes.py index 06a0cb4..36efe49 100644 --- a/ch10-primer-on-oop/3-inherit-from-other-classes.py +++ b/ch10-primer-on-oop/3-inherit-from-other-classes.py @@ -41,4 +41,7 @@ def __init__(self, side_length): square = Square(4) -print(square.area()) +print(square.area()) # 16 + +square.width = 5 # Modifies .width but not .length +print(square.area()) # 20 From fe1ec67dd6aa5202b09c2fa4d30b531f168e753f Mon Sep 17 00:00:00 2001 From: Martin Breuss Date: Tue, 12 Dec 2023 13:10:34 +0100 Subject: [PATCH 7/7] Fix typo --- .../1-tuples-are-immutable-sequences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6f0f1ff..1b0bcf0 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 @@ -23,7 +23,7 @@ # Create a tuple containing the letters of your name from a string my_name = tuple("David") -# Exercide 5 +# Exercise 5 # Check whether or not x is in my_name print("x" in my_name)