From f3bba0c4860ce6757b8e188ec820c59a11624143 Mon Sep 17 00:00:00 2001 From: harininr <145009229+harininr@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:43:20 +0530 Subject: [PATCH 1/3] docs: clarify learning objective for run-in-circles exercise --- ch06-functions-and-loops/4-run-in-circles.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch06-functions-and-loops/4-run-in-circles.py b/ch06-functions-and-loops/4-run-in-circles.py index edd9d0e..1706b50 100644 --- a/ch06-functions-and-loops/4-run-in-circles.py +++ b/ch06-functions-and-loops/4-run-in-circles.py @@ -1,6 +1,9 @@ # 6.4 - Run in Circles # Solutions to review exercises +# This exercise focuses on using a for loop to repeat an action +# a specific number of times. Pay attention to how the loop +# variable changes on each iteration. # Exercise 1 # print the integer 2 through 10 using a "for" loop From fe85aaf65693a939575b31c7f0bf38d582d63c96 Mon Sep 17 00:00:00 2001 From: harininr <145009229+harininr@users.noreply.github.com> Date: Mon, 15 Dec 2025 23:47:44 +0530 Subject: [PATCH 2/3] feat: add input validation to temperature conversion exercise --- .../3-challenge-convert-temperatures.py | 28 ++++++++++--------- ch06-functions-and-loops/4-run-in-circles.py | 3 -- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/ch06-functions-and-loops/3-challenge-convert-temperatures.py b/ch06-functions-and-loops/3-challenge-convert-temperatures.py index 21184be..f653623 100644 --- a/ch06-functions-and-loops/3-challenge-convert-temperatures.py +++ b/ch06-functions-and-loops/3-challenge-convert-temperatures.py @@ -2,37 +2,39 @@ # Solution to challenge +def get_numeric_input(prompt): + """Prompt the user until a valid numeric input is entered.""" + while True: + try: + return float(input(prompt)) + except ValueError: + print("Invalid input. Please enter a numeric value.") + + def convert_cel_to_far(temp_cel): """Return the Celsius temperature temp_cel converted to Fahrenheit.""" - temp_far = temp_cel * (9 / 5) + 32 - return temp_far + return temp_cel * (9 / 5) + 32 def convert_far_to_cel(temp_far): """Return the Fahrenheit temperature temp_far converted to Celsius.""" - temp_cel = (temp_far - 32) * (5 / 9) - return temp_cel + return (temp_far - 32) * (5 / 9) # Prompt the user to input a Fahrenheit temperature. -temp_far = input("Enter a temperature in degrees F: ") +temp_far = get_numeric_input("Enter a temperature in degrees F: ") # Convert the temperature to Celsius. -# Note that `temp_far` must be converted to a `float` -# since `input()` returns a string. -temp_cel = convert_far_to_cel(float(temp_far)) +temp_cel = convert_far_to_cel(temp_far) # Display the converted temperature print(f"{temp_far} degrees F = {temp_cel:.2f} degrees C") -# You could also use `round()` instead of the formatting mini-language: -# print(f"{temp_far} degrees F = {round(temp_cel, 2)} degrees C"") - # Prompt the user to input a Celsius temperature. -temp_cel = input("\nEnter a temperature in degrees C: ") +temp_cel = get_numeric_input("\nEnter a temperature in degrees C: ") # Convert the temperature to Fahrenheit. -temp_far = convert_cel_to_far(float(temp_cel)) +temp_far = convert_cel_to_far(temp_cel) # Display the converted temperature print(f"{temp_cel} degrees C = {temp_far:.2f} degrees F") diff --git a/ch06-functions-and-loops/4-run-in-circles.py b/ch06-functions-and-loops/4-run-in-circles.py index 1706b50..edd9d0e 100644 --- a/ch06-functions-and-loops/4-run-in-circles.py +++ b/ch06-functions-and-loops/4-run-in-circles.py @@ -1,9 +1,6 @@ # 6.4 - Run in Circles # Solutions to review exercises -# This exercise focuses on using a for loop to repeat an action -# a specific number of times. Pay attention to how the loop -# variable changes on each iteration. # Exercise 1 # print the integer 2 through 10 using a "for" loop From 9a719ba63534b4dfbd56c84322c60a03efdf4493 Mon Sep 17 00:00:00 2001 From: harininr <145009229+harininr@users.noreply.github.com> Date: Mon, 15 Dec 2025 23:53:24 +0530 Subject: [PATCH 3/3] fix: remove duplicate return statements in temperature conversion --- ch06-functions-and-loops/3-challenge-convert-temperatures.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ch06-functions-and-loops/3-challenge-convert-temperatures.py b/ch06-functions-and-loops/3-challenge-convert-temperatures.py index f653623..c7e0db9 100644 --- a/ch06-functions-and-loops/3-challenge-convert-temperatures.py +++ b/ch06-functions-and-loops/3-challenge-convert-temperatures.py @@ -21,6 +21,7 @@ def convert_far_to_cel(temp_far): return (temp_far - 32) * (5 / 9) + # Prompt the user to input a Fahrenheit temperature. temp_far = get_numeric_input("Enter a temperature in degrees F: ")