Skip to content

Commit 1fb92e7

Browse files
committed
added conversions between celsius and fahrenheit
1 parent b6ca263 commit 1fb92e7

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
""" Convert temparature from Celsius to Fahrenheit """
2+
3+
def celsius_to_fahrenheit(celsius):
4+
"""
5+
Convert a given value from Celsius to Fahrenheit
6+
7+
>>> celsius_to_fahrenheit(-40)
8+
-40.0
9+
>>> celsius_to_fahrenheit(-20)
10+
-4.0
11+
>>> celsius_to_fahrenheit(0)
12+
32.0
13+
>>> celsius_to_fahrenheit(20)
14+
68.0
15+
>>> celsius_to_fahrenheit(40)
16+
104.0
17+
>>> celsius_to_fahrenheit("40")
18+
Traceback (most recent call last):
19+
...
20+
TypeError: 'str' object cannot be interpreted as integer
21+
"""
22+
23+
if type(celsius) == str:
24+
"""
25+
Check whether given value is string and raise Type Error
26+
"""
27+
raise TypeError("'str' object cannot be interpreted as integer")
28+
29+
fahrenheit = (celsius * 9/5) + 32 # value converted from celsius to fahrenheit
30+
print(fahrenheit)
31+
32+
if __name__ == "__main__":
33+
import doctest
34+
doctest.testmod()

conversions/fahrenheit_to_celsius.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
""" Convert temparature from Fahrenheit to Celsius """
2+
3+
def fahrenheit_to_celsius(fahrenheit):
4+
"""
5+
Convert a given value from Fahrenheit to Celsius and round it to 2 d.p.
6+
7+
>>> fahrenheit_to_celsius(0)
8+
-17.78
9+
>>> fahrenheit_to_celsius(20)
10+
-6.67
11+
>>> fahrenheit_to_celsius(40)
12+
4.44
13+
>>> fahrenheit_to_celsius(60)
14+
15.56
15+
>>> fahrenheit_to_celsius(80)
16+
26.67
17+
>>> fahrenheit_to_celsius(100)
18+
37.78
19+
>>> fahrenheit_to_celsius("100")
20+
Traceback (most recent call last):
21+
...
22+
TypeError: 'str' object cannot be interpreted as integer
23+
"""
24+
if type(fahrenheit) == str:
25+
"""
26+
Check whether given value is string and raise Type Error
27+
"""
28+
raise TypeError("'str' object cannot be interpreted as integer")
29+
30+
31+
celsius = (fahrenheit - 32)*5/9 # value converted from fahrenheit to celsius
32+
celsius = round(celsius, 2) # converted (celsius) value is rounded to two decimal places
33+
print(celsius)
34+
35+
if __name__ == "__main__":
36+
import doctest
37+
doctest.testmod()
38+

0 commit comments

Comments
 (0)