File tree 2 files changed +9
-20
lines changed 2 files changed +9
-20
lines changed Original file line number Diff line number Diff line change 1
1
""" Convert temperature from Celsius to Fahrenheit """
2
2
3
3
4
- def celsius_to_fahrenheit (celsius ) :
4
+ def celsius_to_fahrenheit (celsius : float ) -> float :
5
5
"""
6
- Convert a given value from Celsius to Fahrenheit
6
+ Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
7
7
8
8
>>> print(celsius_to_fahrenheit(-40))
9
9
-40.0
@@ -15,20 +15,14 @@ def celsius_to_fahrenheit(celsius):
15
15
68.0
16
16
>>> print(celsius_to_fahrenheit(40))
17
17
104.0
18
- >>> print(celsius_to_fahrenheit("40 "))
18
+ >>> print(celsius_to_fahrenheit("celsius "))
19
19
Traceback (most recent call last):
20
20
...
21
- TypeError: 'str' object cannot be interpreted as integer
21
+ ValueError: could not convert string to float: 'celsius'
22
22
"""
23
23
24
- if type (celsius ) == 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
- fahrenheit = (celsius * 9 / 5 ) + 32 # value converted from celsius to fahrenheit
31
- return fahrenheit
24
+ celsius = float (celsius )
25
+ return round ((celsius * 9 / 5 ) + 32 , 2 )
32
26
33
27
34
28
if __name__ == "__main__" :
Original file line number Diff line number Diff line change @@ -17,17 +17,12 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float:
17
17
26.67
18
18
>>> print(fahrenheit_to_celsius(100))
19
19
37.78
20
- >>> print(fahrenheit_to_celsius("100 "))
20
+ >>> print(fahrenheit_to_celsius("fahrenheit "))
21
21
Traceback (most recent call last):
22
22
...
23
- TypeError: 'str' object cannot be interpreted as integer
23
+ ValueError: could not convert string to float: 'fahrenheit'
24
24
"""
25
- if type (fahrenheit ) == str :
26
- """
27
- Check whether given value is string and raise Type Error
28
- """
29
- raise TypeError ("'str' object cannot be interpreted as integer" )
30
-
25
+ fahrenheit = float (fahrenheit )
31
26
return round ((fahrenheit - 32 ) * 5 / 9 , 2 )
32
27
33
28
You can’t perform that action at this time.
0 commit comments