Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added conversions between celsius and fahrenheit
  • Loading branch information
karimzakir02 committed Jul 8, 2020
commit 1fb92e76981872f456a957c3fbb5b70c1ef7c8cc
34 changes: 34 additions & 0 deletions conversions/celsius_fahrenheit_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
""" Convert temparature from Celsius to Fahrenheit """

def celsius_to_fahrenheit(celsius):
"""
Convert a given value from Celsius to Fahrenheit

>>> celsius_to_fahrenheit(-40)
-40.0
>>> celsius_to_fahrenheit(-20)
-4.0
>>> celsius_to_fahrenheit(0)
32.0
>>> celsius_to_fahrenheit(20)
68.0
>>> celsius_to_fahrenheit(40)
104.0
>>> celsius_to_fahrenheit("40")
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as integer
"""

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)

if __name__ == "__main__":
import doctest
doctest.testmod()
38 changes: 38 additions & 0 deletions conversions/fahrenheit_to_celsius.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
""" Convert temparature from Fahrenheit to Celsius """

def fahrenheit_to_celsius(fahrenheit):
"""
Convert a given value from Fahrenheit to Celsius and round it to 2 d.p.

>>> fahrenheit_to_celsius(0)
-17.78
>>> fahrenheit_to_celsius(20)
-6.67
>>> fahrenheit_to_celsius(40)
4.44
>>> fahrenheit_to_celsius(60)
15.56
>>> fahrenheit_to_celsius(80)
26.67
>>> fahrenheit_to_celsius(100)
37.78
>>> fahrenheit_to_celsius("100")
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as integer
"""
if type(fahrenheit) == str:
"""
Check whether given value is string and raise Type Error
"""
raise TypeError("'str' object cannot be interpreted as integer")


celsius = (fahrenheit - 32)*5/9 # value converted from fahrenheit to celsius
celsius = round(celsius, 2) # converted (celsius) value is rounded to two decimal places
print(celsius)

if __name__ == "__main__":
import doctest
doctest.testmod()