forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from MIT-Emerging-Talent/Temperature_Conversio…
…n_23 challenge_23 : about temperature_conversion
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""conversion of temperature from celsius to fahrenheit.""" | ||
|
||
|
||
def celsius_to_fahrenheit(celsius_list): | ||
""" | ||
Convert a list of temperatures from Celsius to Fahrenheit. | ||
Args: | ||
celsius_list (list): List of temperatures in Celsius. | ||
Returns: | ||
list: List of temperatures converted to Fahrenheit. | ||
Raises: | ||
ValueError: If input is not a list or contains invalid elements. | ||
>>> celsius_to_fahrenheit([0, 100]) | ||
[32.0, 212.0] | ||
>>> celsius_to_fahrenheit([-40, -10]) | ||
[-40.0, 14.0] | ||
>>> celsius_to_fahrenheit([25.5, 0.5]) | ||
[77.9, 32.9] | ||
""" | ||
|
||
if not isinstance(celsius_list, list): | ||
raise ValueError("Input must be a list.") | ||
|
||
fahrenheit_list = [] | ||
|
||
for temp in celsius_list: | ||
if not isinstance(temp, (int, float)): | ||
raise ValueError("List elements must be integers or floats.") | ||
|
||
fahrenheit = (9 / 5) * temp + 32 | ||
fahrenheit_list.append(round(fahrenheit, 2)) | ||
|
||
return fahrenheit_list | ||
|
||
|
||
if __name__ == "__main__": | ||
user_input = input("Enter the temperature in celsius: ") | ||
|
||
input_celsius_list = [] | ||
if not user_input.strip(): | ||
print("Error: No input provided.") | ||
else: | ||
try: | ||
input_celsius_list = [float(temp.strip()) for temp in user_input.split(",")] | ||
except ValueError: | ||
print( | ||
"Error: Invalid input. Please enter a list of numbers separated by commas." | ||
) | ||
input_celsius_list = [] | ||
|
||
try: | ||
result = celsius_to_fahrenheit(input_celsius_list) | ||
print("The list of temperature in fahrenheit is:", result) | ||
except ValueError as e: | ||
print("Error:", e) |
36 changes: 36 additions & 0 deletions
36
solutions/tests/challenge_23/test_celsius_to_fahrenheit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Test the celsius_to_fahrenheit function from the challenge_23 module.""" | ||
|
||
import unittest | ||
from solutions.challenge_23.celsius_to_fahrenheit import celsius_to_fahrenheit | ||
|
||
|
||
class TestCelsiusToFahrenheit(unittest.TestCase): | ||
"""This tests module provide test for the celsius_to_fahrenheit function. | ||
It tests the function with different inputs and checks if the output is as expected.""" | ||
|
||
def test__with_positive_temperatures(self): | ||
"""Test the program with positive temperature list.""" | ||
self.assertEqual(celsius_to_fahrenheit([0, 100]), [32.0, 212.0]) | ||
|
||
def test_with_negative_temperatures(self): | ||
"""Test the program with negative temperatures list.""" | ||
self.assertEqual(celsius_to_fahrenheit([-40, -10]), [-40.0, 14.0]) | ||
|
||
def test_with_mixed_temperatures(self): | ||
"""Test the program with mixed temperatures list""" | ||
self.assertEqual(celsius_to_fahrenheit([-10, 0, 25]), [14.0, 32.0, 77.0]) | ||
|
||
def test_float_temperatures(self): | ||
"""Test the program with float temperatures.""" | ||
result = celsius_to_fahrenheit([0.5, -0.5]) | ||
self.assertAlmostEqual(result[0], 32.9, places=1) | ||
|
||
def test_invalid_input_non_list(self): | ||
"""Test invalid input that is not a list.""" | ||
with self.assertRaises(ValueError): | ||
celsius_to_fahrenheit("not a list") | ||
|
||
def test_invalid_input_non_numeric_elements(self): | ||
"""Test the program with invalid input that contains non-numeric elements.""" | ||
with self.assertRaises(ValueError): | ||
celsius_to_fahrenheit([30, "a string", 45]) |