From 026379d0cdd68df5c80f71f921e531ecdc6557e6 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 13 Jun 2022 10:28:44 -0700 Subject: [PATCH 01/15] Add number guessing exercise solution --- .../3-control-the-flow-of-your-program.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ch08-conditional-logic/3-control-the-flow-of-your-program.py b/ch08-conditional-logic/3-control-the-flow-of-your-program.py index 2f19b72..be6fa62 100644 --- a/ch08-conditional-logic/3-control-the-flow-of-your-program.py +++ b/ch08-conditional-logic/3-control-the-flow-of-your-program.py @@ -2,6 +2,7 @@ # Solutions to review exercises +# --- Exercise 1 # Display whether the length of user input is <, > or = 5 characters my_input = input("Type something: ") @@ -12,3 +13,15 @@ print("Your input is greater than 5 characters long.") else: print("Your input is 5 characters long.") + + +# --- Exercise 2 +# Number guessing program ("guess" the number 3) + +print("I'm thinking of a number between 1 and 10. Guess which one.") +my_guess = input("Type in your guess: ") + +if my_guess == "3": + print("You win!") +else: + print("You lose.") From b0aff3ebceff944900379f9df20d4a6acab3bd58 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 13 Jun 2022 10:33:40 -0700 Subject: [PATCH 02/15] Cleanup --- ch06-functions-and-loops/2-write-your-own-functions.py | 2 +- ch08-conditional-logic/2-add-some-logic.py | 4 ++-- ch08-conditional-logic/3-control-the-flow-of-your-program.py | 4 ++-- ch12-file-input-and-output/3-common-file-system-operations.py | 3 ++- .../1-use-numpy-for-matrix-manipulation.py | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ch06-functions-and-loops/2-write-your-own-functions.py b/ch06-functions-and-loops/2-write-your-own-functions.py index d21c159..ac29db5 100644 --- a/ch06-functions-and-loops/2-write-your-own-functions.py +++ b/ch06-functions-and-loops/2-write-your-own-functions.py @@ -5,7 +5,7 @@ # Exercise 1 def cube(num): """Return the cube of the input number.""" - cube_num = num ** 3 # Could also use pow(num, 3) + cube_num = num**3 # Could also use pow(num, 3) return cube_num diff --git a/ch08-conditional-logic/2-add-some-logic.py b/ch08-conditional-logic/2-add-some-logic.py index 2880c21..e7bd650 100644 --- a/ch08-conditional-logic/2-add-some-logic.py +++ b/ch08-conditional-logic/2-add-some-logic.py @@ -1,14 +1,14 @@ # 8.2 - Add Some Logic # Solutions to review exercises -# --- Exercise 1 +# Exercise 1 # Test whether these expressions are True or False print((1 <= 1) and (1 != 1)) print(not (1 != 2)) print(("good" != "bad") or False) print(("good" != "Good") and not (1 == 1)) -# --- Exercise 2 +# Exercise 2 # Add parentheses so that the following expressions all # evaluate to True diff --git a/ch08-conditional-logic/3-control-the-flow-of-your-program.py b/ch08-conditional-logic/3-control-the-flow-of-your-program.py index be6fa62..6e81837 100644 --- a/ch08-conditional-logic/3-control-the-flow-of-your-program.py +++ b/ch08-conditional-logic/3-control-the-flow-of-your-program.py @@ -2,7 +2,7 @@ # Solutions to review exercises -# --- Exercise 1 +# Exercise 1 # Display whether the length of user input is <, > or = 5 characters my_input = input("Type something: ") @@ -15,7 +15,7 @@ print("Your input is 5 characters long.") -# --- Exercise 2 +# Exercise 2 # Number guessing program ("guess" the number 3) print("I'm thinking of a number between 1 and 10. Guess which one.") diff --git a/ch12-file-input-and-output/3-common-file-system-operations.py b/ch12-file-input-and-output/3-common-file-system-operations.py index 73c506b..eaf8042 100644 --- a/ch12-file-input-and-output/3-common-file-system-operations.py +++ b/ch12-file-input-and-output/3-common-file-system-operations.py @@ -29,6 +29,7 @@ file1.unlink() -# # Exercise 5 +# Exercise 5 import shutil + shutil.rmtree(new_dir) diff --git a/ch17-scientific-computing-and-graphing/1-use-numpy-for-matrix-manipulation.py b/ch17-scientific-computing-and-graphing/1-use-numpy-for-matrix-manipulation.py index 28e0555..20a3307 100644 --- a/ch17-scientific-computing-and-graphing/1-use-numpy-for-matrix-manipulation.py +++ b/ch17-scientific-computing-and-graphing/1-use-numpy-for-matrix-manipulation.py @@ -17,7 +17,7 @@ # Exercise 3 # Square every entry and save in a new matrix -second_matrix = first_matrix ** 2 +second_matrix = first_matrix**2 # Exercise 4 # Put first_matrix on top of second_matrix From 64368160573eae71c1432b352745619a838a3228 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Tue, 19 Jul 2022 08:21:54 -0700 Subject: [PATCH 03/15] Update 2-lists-are-mutable-sequences.py --- .../2-lists-are-mutable-sequences.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch09-lists-tuples-and-dictionaries/2-lists-are-mutable-sequences.py b/ch09-lists-tuples-and-dictionaries/2-lists-are-mutable-sequences.py index 311fdb4..6151b2b 100644 --- a/ch09-lists-tuples-and-dictionaries/2-lists-are-mutable-sequences.py +++ b/ch09-lists-tuples-and-dictionaries/2-lists-are-mutable-sequences.py @@ -8,8 +8,8 @@ # Exercise 2 -# Append the string "broccolo" to the food list using .append() -food.append("brocolli") +# Append the string "broccoli" to the food list using .append() +food.append("broccoli") # Exercise 3 From 06c7585e8cf5e9bb212371f249df5d7500df3801 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 1 Aug 2022 08:36:29 -0700 Subject: [PATCH 04/15] Update 3-nesting-sorting-and-copying-lists-and-tuples.py --- .../3-nesting-sorting-and-copying-lists-and-tuples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py b/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py index f9a3121..8ca8a3e 100644 --- a/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py +++ b/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py @@ -8,7 +8,7 @@ # Exercise 2 -# Loop over data a print the sum of each nested tuple +# Loop over data and print the sum of each nested tuple for i in range(len(data)): print(f"Row {i+1} sum: {data[i][0] + data[i][1]}") From 9f5dd66cd723f397e028f071d336e5c836854824 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 1 Aug 2022 10:07:03 -0700 Subject: [PATCH 05/15] Update 3-nesting-sorting-and-copying-lists-and-tuples.py --- .../3-nesting-sorting-and-copying-lists-and-tuples.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py b/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py index 8ca8a3e..a7604b3 100644 --- a/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py +++ b/ch09-lists-tuples-and-dictionaries/3-nesting-sorting-and-copying-lists-and-tuples.py @@ -9,8 +9,10 @@ # Exercise 2 # Loop over data and print the sum of each nested tuple -for i in range(len(data)): - print(f"Row {i+1} sum: {data[i][0] + data[i][1]}") +index = 1 +for row in data: + print(f"Row {index} sum: {sum(row)}") + index += 1 # Exercise 3 From dd6d58e611b2dfc76d73323bc65b072ffc8ee2d5 Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 1 Aug 2022 10:07:24 -0700 Subject: [PATCH 06/15] Update 4-challenge-list-of-lists.py --- ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py | 1 - 1 file changed, 1 deletion(-) 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 da8ce92..ccccb6c 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 @@ -3,7 +3,6 @@ def enrollment_stats(list_of_universities): - # Variables total_students = [] total_tuition = [] From 730c3dee338e6a87c96d789738895e9c49681a1b Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Mon, 1 Aug 2022 10:15:17 -0700 Subject: [PATCH 07/15] Remove \ line breaks in file paths --- ch14-interact-with-pdf-files/1-extract-text-from-a-pdf.py | 3 +-- ch14-interact-with-pdf-files/2-extract-pages-from-a-pdf.py | 3 +-- .../4-concatenating-and-merging-pdfs.py | 3 +-- .../5-rotating-and-cropping-pdf-pages.py | 3 +-- .../6-encrypting-and-decrypting-pdfs.py | 3 +-- ch14-interact-with-pdf-files/7-challenge-unscramble-a-pdf.py | 3 +-- .../2-use-matplotlib-for-plotting-graphs.py | 3 +-- 7 files changed, 7 insertions(+), 14 deletions(-) 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 afc156d..857ac36 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 @@ -18,8 +18,7 @@ # 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 # 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.home() / "python-basics-exercises/ch14-interact-with-pdf-files/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/2-extract-pages-from-a-pdf.py b/ch14-interact-with-pdf-files/2-extract-pages-from-a-pdf.py index dc71233..fc34b2b 100644 --- a/ch14-interact-with-pdf-files/2-extract-pages-from-a-pdf.py +++ b/ch14-interact-with-pdf-files/2-extract-pages-from-a-pdf.py @@ -16,8 +16,7 @@ # downloaded the solutions folder and extracted it into the home # directory on your computer. 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/Pride_and_Prejudice.pdf" +pdf_path = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/Pride_and_Prejudice.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 452c10d..d471a17 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,8 +15,7 @@ from PyPDF2 import PdfFileMerger -BASE_PATH = Path.home() / "python-basics-exercises/" \ - "ch14-interact-with-pdf-files/practice_files" +BASE_PATH = Path.home() / "python-basics-exercises/ch14-interact-with-pdf-files/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 0c4d311..4bb60b5 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,8 +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.home() / "python-basics-exercises/ch14-interact-with-pdf-files/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 05c0d3d..9db418a 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,8 +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.home() / "python-basics-exercises/ch14-interact-with-pdf-files/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 07bd35d..d66e6a8 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,8 +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.home() / "python-basics-exercises/ch14-interact-with-pdf-files/practice_files/scrambled.pdf" pdf_reader = PdfFileReader(str(pdf_path)) pdf_writer = PdfFileWriter() diff --git a/ch17-scientific-computing-and-graphing/2-use-matplotlib-for-plotting-graphs.py b/ch17-scientific-computing-and-graphing/2-use-matplotlib-for-plotting-graphs.py index 63a805b..3afa8eb 100644 --- a/ch17-scientific-computing-and-graphing/2-use-matplotlib-for-plotting-graphs.py +++ b/ch17-scientific-computing-and-graphing/2-use-matplotlib-for-plotting-graphs.py @@ -8,8 +8,7 @@ import os # Change `path` to actual path on your system -path = "C:/Real Python/python-basics-exercises/ch16-scientific-computing-and-graphing/\ -practice_files" +path = "C:/Real Python/python-basics-exercises/ch16-scientific-computing-and-graphing/practice_files" years = [] temperatures = [] From 5ccdff76df036f3277cf6119f39de0858380cf6c Mon Sep 17 00:00:00 2001 From: Dan Bader Date: Tue, 13 Sep 2022 14:39:52 -0700 Subject: [PATCH 08/15] Update ch04 exercise copy --- ...e-your-print-statements.py => 7-streamline-your-prints.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename ch04-strings-and-string-methods/{7-streamline-your-print-statements.py => 7-streamline-your-prints.py} (81%) diff --git a/ch04-strings-and-string-methods/7-streamline-your-print-statements.py b/ch04-strings-and-string-methods/7-streamline-your-prints.py similarity index 81% rename from ch04-strings-and-string-methods/7-streamline-your-print-statements.py rename to ch04-strings-and-string-methods/7-streamline-your-prints.py index 18c29c4..d89832c 100644 --- a/ch04-strings-and-string-methods/7-streamline-your-print-statements.py +++ b/ch04-strings-and-string-methods/7-streamline-your-prints.py @@ -1,4 +1,4 @@ -# 4.7 - Streamline Your Print Statements +# 4.7 - Streamline Your Prints # Solutions to review exercies @@ -6,7 +6,7 @@ weight = 0.2 animal = "newt" -# Concatenate a number and a string in one print statement +# Concatenate a number and a string in one print call print(str(weight) + " kg is the weight of the " + animal + ".") From f2ffc6550e84ab82e891c77591312a1024b0704b Mon Sep 17 00:00:00 2001 From: ybrenning Date: Thu, 15 Sep 2022 17:17:56 +0200 Subject: [PATCH 09/15] 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 10/15] 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 11/15] 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 12/15] 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 13/15] 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 14/15] 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 15/15] 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)