Untitled Document (20)
Untitled Document (20)
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
def celsius_to_kelvin(celsius):
return celsius + 273.15
def kelvin_to_celsius(kelvin):
return kelvin - 273.15
def fahrenheit_to_kelvin(fahrenheit):
return celsius_to_kelvin(fahrenheit_to_celsius(fahrenheit))
def kelvin_to_fahrenheit(kelvin):
return celsius_to_fahrenheit(kelvin_to_celsius(kelvin))
def menu():
print("Welcome to the temperature calculator!")
print("Press 1 for celsius to fahrenheit")
print("Press 2 for fahrenheit to celsius")
print("Press 3 for celsius to kelvin")
print("Press 4 for kelvin to celsius")
print("Press 5 for fahrenheit to kelvin")
print("Press 6 for kelvin to fahrenheit")
print("Press 7 to exit")
def main_menu(choice=None):
while True:
if choice is None:
menu()
choice = int(input("Enter a number between 1 and 7: "))
if choice == 1:
result = celsius_to_fahrenheit(temperature)
print(str(temperature) + " °C is equal to " + str(round(result, 2)) + " °F")
elif choice == 2:
result = fahrenheit_to_celsius(temperature)
print(str(temperature) + " °F is equal to " + str(round(result, 2)) + " °C")
elif choice == 3:
result = celsius_to_kelvin(temperature)
print(str(temperature) + " °C is equal to " + str(round(result, 2)) + " K")
elif choice == 4:
result = kelvin_to_celsius(temperature)
print(str(temperature) + " K is equal to " + str(round(result, 2)) + " °C")
elif choice == 5:
result = fahrenheit_to_kelvin(temperature)
print(str(temperature) + " °F is equal to " + str(round(result, 2)) + " K")
elif choice == 6:
result = kelvin_to_fahrenheit(temperature)
print(str(temperature) + " K is equal to " + str(round(result, 2)) + " °F")
choice = None
main_menu()