Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions conversions/celsius_to_fahrenheit.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
""" Convert temperature from Celsius to Fahrenheit """


def celsius_to_fahrenheit(celsius):
def celsius_to_fahrenheit(celsius: float) -> float:
"""
Convert a given value from Celsius to Fahrenheit
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
>>> celsius_to_fahrenheit(-40)
>>> print(celsius_to_fahrenheit(-40))
-40.0
>>> celsius_to_fahrenheit(-20)
>>> print(celsius_to_fahrenheit(-20))
-4.0
>>> celsius_to_fahrenheit(0)
>>> print(celsius_to_fahrenheit(0))
32.0
>>> celsius_to_fahrenheit(20)
>>> print(celsius_to_fahrenheit(20))
68.0
>>> celsius_to_fahrenheit(40)
>>> print(celsius_to_fahrenheit(40))
104.0
>>> celsius_to_fahrenheit("40")
>>> print(celsius_to_fahrenheit("celsius"))
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as integer
ValueError: could not convert string to float: 'celsius'
"""

if type(celsius) == str:
"""
Check whether given value is string and raise Type Error
"""
raise TypeError("'str' object cannot be interpreted as integer")

fahrenheit = (celsius * 9 / 5) + 32 # value converted from celsius to fahrenheit
print(fahrenheit)
celsius = float(celsius)
return round((celsius * 9 / 5) + 32, 2)


if __name__ == "__main__":
Expand Down
25 changes: 10 additions & 15 deletions conversions/fahrenheit_to_celsius.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@

def fahrenheit_to_celsius(fahrenheit: float) -> float:
"""
Convert a given value from Fahrenheit to Celsius and round it to 2 d.p.
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.
>>> fahrenheit_to_celsius(0)
>>> print(fahrenheit_to_celsius(0))
-17.78
>>> fahrenheit_to_celsius(20)
>>> print(fahrenheit_to_celsius(20))
-6.67
>>> fahrenheit_to_celsius(40)
>>> print(fahrenheit_to_celsius(40))
4.44
>>> fahrenheit_to_celsius(60)
>>> print(fahrenheit_to_celsius(60))
15.56
>>> fahrenheit_to_celsius(80)
>>> print(fahrenheit_to_celsius(80))
26.67
>>> fahrenheit_to_celsius(100)
>>> print(fahrenheit_to_celsius(100))
37.78
>>> fahrenheit_to_celsius("100")
>>> print(fahrenheit_to_celsius("fahrenheit"))
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as integer
ValueError: could not convert string to float: 'fahrenheit'
"""
if type(fahrenheit) == str:
"""
Check whether given value is string and raise Type Error
"""
raise TypeError("'str' object cannot be interpreted as integer")

fahrenheit = float(fahrenheit)
return round((fahrenheit - 32) * 5 / 9, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fahrenheit = float(fahrenheit)
return round((fahrenheit - 32) * 5 / 9, 2)
return round((float(fahrenheit) - 32) * 5 / 9, 2)



Expand Down