-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemp_converter.py
More file actions
55 lines (45 loc) · 1.76 KB
/
Temp_converter.py
File metadata and controls
55 lines (45 loc) · 1.76 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
46
47
48
49
50
51
52
53
54
55
"""
Thos program converts temperature from Celsius to Fahrenheit and vice versa.
The user is presented with a menu system that allows them to select the conversion they would like to perform.
The program then prompts the user to enter the temperature they would like to convert and displays the result.
Copyright: Adam Nix (2024)
"""
def menu_system():
"""
Displays the menu system and returns the user's choice.
"""
print(
"welcome to my temperature converter. Please enter an option for the following list "
)
print("select 1 for °C to °F")
print("select 2 for °F to °C")
while True:
try:
choice = int(input("Please enter your choice: "))
except ValueError:
print("Invalid choice (please select 1 or 2)")
continue
if choice == 1:
print("You have selected °C to °F")
return celsius_to_fahrenheit()
elif choice == 2:
print("You have selected °F to °C")
return fahrenheit_to_celsius()
else:
print("Invalid choice (please select 1 or 2)")
def celsius_to_fahrenheit():
"""
Converts temperature from Celsius to Fahrenheit.
"""
celsius = float(input("Please enter the temperature in Celsius: "))
fahrenheit = round((celsius * 9 / 5) + 32, 2)
print("The temperature in Fahrenheit is", fahrenheit, "°F")
def fahrenheit_to_celsius():
"""
Converts temperature from Fahrenheit to Celsius.
"""
fahrenheit = float(input("Please enter the temperature in Fahrenheit: "))
celsius = round((fahrenheit - 32) * 5 / 9, 2)
print("The temperature in Celsius is", celsius, "°C")
if __name__ == "__main__":
menu_system()