-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyhton_Programming_Fundamentals.py
More file actions
45 lines (35 loc) · 1.68 KB
/
Pyhton_Programming_Fundamentals.py
File metadata and controls
45 lines (35 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Functional code block: Temperature conversion tool
Your task:
Write three Python functions that implement a simple temperature conversion tool. Be careful to spell the method names correctly.
celsius_to_fahrenheit(celsius): Takes a temperature in Celsius and converts it to Fahrenheit.
fahrenheit_to_celsius(fahrenheit): Takes a temperature in Fahrenheit and converts it to Celsius.
convert_temperature(temperature, unit): Takes a temperature value and a unit ('C' for Celsius or 'F' for Fahrenheit). It calls the appropriate conversion function based on the unit and returns the converted temperature.
Tips:
Remember these formulas:
fahrenheit = (celsius * 9/5) + 32
celsius = (fahrenheit - 32) * 5/9
Make sure you are returning values as floating-point numbers; do not round.
"""
# Add your code here
def celsius_to_fahrenheit(celsius):
"""Takes a temperature in Celsius and converts it to Fahrenheit."""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
## Test celsius to fahrenheit
print(celsius_to_fahrenheit(25))
def fahrenheit_to_celsius(fahrenheit):
"""Takes a temperature in Celsius and converts it to Fahrenheit."""
celsius = (fahrenheit - 32) * 5/9
return celsius
## Test fahrenheit to celsius
print(fahrenheit_to_celsius(77))
def convert_temperature(temperature, unit):
""" Takes a temperature value and a unit ('C' for Celsius or 'F' for Fahrenheit). It calls the appropriate conversion function based on the unit and returns the converted temperature."""
if unit == 'C':
return celsius_to_fahrenheit(temperature)
elif unit == 'F':
return fahrenheit_to_celsius(temperature)
else:
print ("invalid unit entered")
return none