تمرین ۱۰:
تبدیل واحدهای دما در پایتون
برنامهای به زبان پایتون بنویسید که دمای داده شده را از سلسیوس (سانتیگراد) به فارنهایت و کلوین، از فارنهایت به سلسیوس و کلوین، و از کلوین به سلسیوس و فارنهایت تبدیل کند.
کد:
def F_C(f):
c = (f - 32) * 5 / 9
return c
def F_K(f):
k = F_C(f)
k = C_K(k)
return k
def K_F(k):
f = K_C(k)
f = C_F(f)
return f
def K_C(k):
c = k - 273.15
return c
def C_F(c):
f = (9 / 5 * c) + 32
return f
def C_K(c):
k = c + 273.15
return k
def Main():
print("Temperature Unit Convertor")
print("Programmer: Mohammad Rajabpur")
print("pythonize.ir")
while True:
print("-----------------------------------")
print("Celsius to Fahrenheit/Kelvin => Base = C")
print("Fahrenheit to Celsius/Kelvin => Base = F")
print("Kelvin to Celsius/Fahrenheit => Base = K")
print("To exit the program => Base = Exit")
b = input("Base = ")
b = b.lower()
if b == "exit":
break
elif b == "c":
print("-----------------------------------")
print("Celsius to Fahrenheit/Kelvin Convertor")
print("To go back to the main panel, type 'back'.\n")
while True:
c = input("Temperature = ")
if c == "back":
break
else:
try:
c = float(c)
print(c, "° C =", C_F(c), "° F =", C_K(c), "° K \n")
except:
print("Error: Please enter a number! \n")
elif b == "f":
print("-----------------------------------")
print("Fahrenheit to Celsius/Kelvin Convertor")
print("To go back to the main panel, type 'back'.\n")
while True:
f = input("Temperature = ")
if f == "back":
break
else:
try:
f = float(f)
print(f, "° F =", F_C(f), "° C =", F_K(f), "° K \n")
except:
print("Error: Please enter a number! \n")
elif b == "k":
print("-----------------------------------")
print("Kelvin to Fahrenheit/Celsius Convertor")
print("To go back to the main panel, type 'back'.\n")
while True:
k = input("Temperature = ")
if k == "back":
break
else:
try:
k = float(k)
print(k, "° K =", K_C(k), "° C =", K_F(k), "° F \n")
except:
print("Error: Please enter a number! \n")
Main()
اجرا:
================== RESTART: C:\Temperature.py ================= Temperature Unit Convertor Programmer: Mohammad Rajabpur pythonize.ir ----------------------------------- Celsius to Fahrenheit/Kelvin => Base = C Fahrenheit to Celsius/Kelvin => Base = F Kelvin to Celsius/Fahrenheit => Base = K To exit the program => Base = Exit Base = c ----------------------------------- Celsius to Fahrenheit/Kelvin Convertor To go back to the main panel, type 'back'. Temperature = -10 -10.0 ° C = 14.0 ° F = 263.15 ° K Temperature = 0 0.0 ° C = 32.0 ° F = 273.15 ° K Temperature = 10 10.0 ° C = 50.0 ° F = 283.15 ° K Temperature = 20 20.0 ° C = 68.0 ° F = 293.15 ° K Temperature = 30 30.0 ° C = 86.0 ° F = 303.15 ° K Temperature = 40 40.0 ° C = 104.0 ° F = 313.15 ° K Temperature = 100 100.0 ° C = 212.0 ° F = 373.15 ° K Temperature = back ----------------------------------- Celsius to Fahrenheit/Kelvin => Base = C Fahrenheit to Celsius/Kelvin => Base = F Kelvin to Celsius/Fahrenheit => Base = K To exit the program => Base = Exit Base = f ----------------------------------- Fahrenheit to Celsius/Kelvin Convertor To go back to the main panel, type 'back'. Temperature = 10 10.0 ° F = -12.222222222222221 ° C = 260.92777777777775 ° K Temperature = 20 20.0 ° F = -6.666666666666667 ° C = 266.4833333333333 ° K Temperature = 30 30.0 ° F = -1.1111111111111112 ° C = 272.0388888888889 ° K Temperature = 32 32.0 ° F = 0.0 ° C = 273.15 ° K Temperature = 40 40.0 ° F = 4.444444444444445 ° C = 277.59444444444443 ° K Temperature = 50 50.0 ° F = 10.0 ° C = 283.15 ° K Temperature = 60 60.0 ° F = 15.555555555555555 ° C = 288.7055555555555 ° K Temperature = 70 70.0 ° F = 21.11111111111111 ° C = 294.26111111111106 ° K Temperature = 80 80.0 ° F = 26.666666666666668 ° C = 299.81666666666666 ° K Temperature = 90 90.0 ° F = 32.22222222222222 ° C = 305.3722222222222 ° K Temperature = 100 100.0 ° F = 37.77777777777778 ° C = 310.92777777777775 ° K Temperature = 0 0.0 ° F = -17.77777777777778 ° C = 255.3722222222222 ° K Temperature = back ----------------------------------- Celsius to Fahrenheit/Kelvin => Base = C Fahrenheit to Celsius/Kelvin => Base = F Kelvin to Celsius/Fahrenheit => Base = K To exit the program => Base = Exit Base = k ----------------------------------- Kelvin to Fahrenheit/Celsius Convertor To go back to the main panel, type 'back'. Temperature = 0 0.0 ° K = -273.15 ° C = -459.66999999999996 ° F Temperature = 273.15 273.15 ° K = 0.0 ° C = 32.0 ° F Temperature = 373.15 373.15 ° K = 100.0 ° C = 212.0 ° F Temperature = back ----------------------------------- Celsius to Fahrenheit/Kelvin => Base = C Fahrenheit to Celsius/Kelvin => Base = F Kelvin to Celsius/Fahrenheit => Base = K To exit the program => Base = Exit Base = exit