我在python中完成了温度转换,并注意到大部分代码都是重复的。我想要的是给它一个更清晰的外观,并认为使用数组和一两个函数我可以将代码行从大约500到150。
下面我将发布一个代码示例。欢迎提出任何建议。
#---Program by Zakar H.---#
#--- TO DO ---#
#1. Fix equations for rankine,delisle, newton, reaumer, and romer.
#2. Code seems repetitive. Maybe use arrays, functions or dictionary.
#Provides an introduction to the game
print("ZvH Temperature Convertor\n")
#Lets user know what values to enter for the conversion type
def convertor():
print("What temperature are you converting from?")
print("Enter 1 for Celcius\nEnter 2 for Fahrenheit\nEnter 3 for Kelvin\nEnter 4 for Rankine")
print("Enter 5 for Delisle\nEnter 6 for Newton\nEnter 7 for Reaumer\nEnter 8 for Romer")
#Gets user input for conversion type
user_input = input('''''')
#If 1 is selected it starts conversion from Celcius
if user_input == ("1"):
def celsius():
print("\nWhat temperature are you converting too?")
print("Enter 1 for Celcius\nEnter 2 for Fahrenheit\nEnter 3 for Kelvin\nEnter 4 for Rankine")
print("Enter 5 for Delisle\nEnter 6 for Newton\nEnter 7 for Reaumer\nEnter 8 for Romer")
#Gets user input for converting
type = input('''''')
#COnverts to celcius
if type == ("1"):
temp = int(input('''\nType the value for conversion.\n'''))
print("\nThe conversion calculated")
print(temp)
#Converts to fahrenheit
elif type == ("2"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = temp * (9.0/5.0) + 32
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Kelvin
elif type == ("3"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = temp + 273.15
print("\nThe conversion calculated")
print(kelvin)
#Converts to Rankine
elif type == ("4"):
temp = int(input('''\nType the value for conversion.\n'''))
rankine = (temp + 273.15) * (9.0/5.0)
print("\nThe conversion calculated")
print(rankine)
#Converts to Delisle
elif type == ("5"):
temp = int(input('''\nType the value for conversion.\n'''))
delisle = (100 - temp) * (3.0/2.0)
print("\nThe conversion calculated")
print(delisle)
#Converts to Newton
elif type == ("6"):
temp = int(input('''\nType the value for conversion.\n'''))
newton = temp * (33.0/100.0)
print("\nThe conversion calculated")
print(newton)
#Converts to Reaumur
elif type == ("7"):
temp = int(input('''\nType the value for conversion.\n'''))
reaumur = temp * (4.0/5.0)
print("\nThe conversion calculated")
print(reaumur)
#Converts to Romer
elif type == ("8"):
temp = int(input('''\nType the value for conversion.\n'''))
romer = temp * (9.0/5.0) + 7.5
print("\nThe conversion calculated")
print(romer)
celsius()
#If 2 is selected it starts conversion from Fahrenheit
if user_input == ("2"):
def fahrenheit():
print("\nWhat temperature are you converting too?")
print("Enter 1 for Celcius\nEnter 2 for Fahrenheit\nEnter 3 for Kelvin\nEnter 4 for Rankine")
print("Enter 5 for Delisle\nEnter 6 for Newton\nEnter 7 for Reaumer\nEnter 8 for Romer")
#Gets user input for converting
fahrenheit_type = input('''''')
#Converts to celcius
if fahrenheit_type == ("1"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = (temp - 32) * (5.0/9.0)
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to fahrenheit
elif fahrenheit_type == ("2"):
temp = int(input('''\nType the value for conversion.\n'''))
print("\nThe conversion calculated")
print(temp)
#Converts to Kelvin
elif fahrenheit_type == ("3"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrneheit = (temp + 459.67) * (5.0/9.0)
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Rankine
elif fahrenheit_type == ("4"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = temp + 459.67
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Delisle
elif fahrenheit_type == ("5"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = (212 - temp) * (5.0/6.0)
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Newton
elif fahrenheit_type == ("6"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = (temp - 32) * (11.0/60.0)
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Reaumur
elif fahrenheit_type == ("7"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = (temp -32) * (4.0/9.0)
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Romer
elif fahrenheit_type == ("8"):
temp = int(input('''\nType the value for conversion.\n'''))
fahrenheit = (temp + 32) * (7.0/24.0) + 32
print("\nThe conversion calculated")
print(fahrenheit)
fahrenheit()
#If 3 is selected it starts conversion from kelvin
if user_input == ("3"):
def kelvin():
print("\nWhat temperature are you converting too?")
print("Enter 1 for Celcius\nEnter 2 for Fahrenheit\nEnter 3 for Kelvin\nEnter 4 for Rankine")
print("Enter 5 for Delisle\nEnter 6 for Newton\nEnter 7 for Reaumer\nEnter 8 for Romer")
#Gets user input for converting
kelvin_type = input('''''')
#Converts to celcius
if kelvin_type == ("1"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = temp - 273.15
print("\nThe conversion calculated")
print(kelvin)
#Converts to fahrenheit
if kelvin_type == ("2"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = temp * (9.0/5.0) - 489.67
print("\nThe conversion calculated")
print(fahrenheit)
#Converts to Kelvin
elif kelvin_type == ("3"):
kelvin = int(input('''\nType the value for conversion.\n'''))
print("\nThe conversion calculated\n")
print(kelvin)
#Converts to Rankine
elif kelvin_type == ("4"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = temp * (9.0/5.0)
print("\nThe conversion calculated")
print(kelvin)
#Converts to Delisle
elif kelvin_type == ("5"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = (375.15 - temp) * (3.0/2.0)
print("\nThe conversion calculated")
print(kelvin)
#Converts to Newton
elif kelvin_type == ("6"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = (temp - 273.15) * (33.0/100.0)
print("\nThe conversion calculated")
print(kelvin)
#Converts to Reaumur
elif kelvin_type == ("7"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = (temp - 273.15) * (4.0/5.0)
print("\nThe conversion calculated")
print(kelvin)
#Converts to Romer
elif kelvin_type == ("8"):
temp = int(input('''\nType the value for conversion.\n'''))
kelvin = (temp - 273.15) * (21.0/40.0) + 7.5
print("\nThe conversion calculated")
print(romer)
kelvin()
答案 0 :(得分:0)
这将是我的第一枪。 警告:我从您的代码中复制粘贴转换,可能会出错;没有检查任何一个。
def fahrenheit_to_celsius(fahrenheit):
return fahrenheit * (9.0/5.0) + 32
def celsius_to_fahrenheit(celsius):
return (celsius - 32) * (5.0/9.0)
def kelvin_to_celsius(kelvin):
return kelvin - 273.15
def fahrenheit_to_kelvin(fahrenheit):
return fahrenheit * (9.0/5.0) - 489.67
convertor_functions = {
'fc': fahrenheit_to_celsius,
'cf': celsius_to_fahrenheit,
'kc': kelvin_to_celsius,
'ck': lambda celsius: celsius + 273.15,
'kf': lambda kelvin: (kelvin + 459.67) * (5.0/9.0)
}
def handle_intput():
'''
read in input of the form "273k in c" return (273, kc)
'''
print("convert temp -- syntax: '46k in f' (empty string to quit)")
input_str = input('convert? ')
if not input_str:
return None
in_temp, target_unit = input_str.split('in')
target_unit = target_unit.strip()
in_temp = in_temp.strip()
orig_unit = in_temp[-1]
orig_temp = float(in_temp[:-1])
convert_str = orig_unit + target_unit
return orig_temp, convert_str
def convert(orig_temp, convert_str):
'''
orig_temp: integer; convert_str: kc (-> kelvin to celsius, etc)
'''
convert_f = convertor_functions[convert_str]
return convert_f(orig_temp)
while True:
ret = handle_intput()
if ret is None:
print('ok, bye!')
break
orig_temp, convert_str = ret
orig_unit = convert_str[0]
target_unit = convert_str[1]
target_temp = convert(orig_temp, convert_str)
print('{}{} = {:3.3f}{}'.format(orig_temp, orig_unit,
target_temp, target_unit))
根据我的口味,将长if/elif/else
序列转换为字典有时是一种很好的做法 - 就像我为你的转换函数所做的那样。 convertor_functions[kc]
现在返回从开尔文转换为摄氏温度的函数,convertor_functions[kc](kelvin)
执行计算。
请注意,转换函数可以轻松地写为lambda
表达式(就像我对'ck'
和'kf'
一样) - 如果您希望代码很短,这可能会有所帮助。< / p>
答案 1 :(得分:0)
temperature_scales = ("Celsius Fahrenheit Kelvin Rankine "
"Delisle Newton Reaumur Romer").split()
def get_number(prompt, *, min=float('-inf'), max=float('inf'), number_type=float):
"""Ask user for a number in [min, max] range."""
while True:
try:
i = number_type(input(prompt))
except ValueError:
pass
else:
if min <= i <= max:
return i
print("Enter %s in the range [%s, %s]" % (number_type.__name__, min, max))
def get_scale(prompt):
"""Ask user for a temperature scale name."""
choices = "\n".join(["Enter %d for %s" % i_v
for i_v in enumerate(temperature_scales, start=1)])
i = get_number("".join([prompt, "\n", choices, "\n? "]),
min=1, max=len(temperature_scales), number_type=int)
return temperature_scales[i-1]
from_scale = get_scale("What temperature scale are you converting from?")
to_scale = get_scale("What temperature scale are you converting to?")
from_temp = get_number("Type the temperature value for conversion\n? ")
to_temp = converter(from_scale, to_scale)(from_temp)
print("The conversion calculated %.2f" % to_temp)
现在,剩下的就是定义converter
函数:
def converter(from_scale, to_scale):
"""Return a function that converts from *from_scale* to *to_scale*."""
def f(temperature):
return from_kelvin[to_scale](to_kelvin[from_scale](temperature))
f.__name__ = "convert%sTo%s" % (from_scale, to_scale)
return f
def celsius_to_kelvin(temperature):
return temperature + 273.15
def celsius_from_kelvin(temperature):
return temperature - 273.15
def fahrenheit_to_kelvin(temperature):
return (temperature + 459.67) * 5 / 9
def fahrenheit_from_kelvin(temperature):
return (temperature * 9 / 5.) - 459.67
from_kelvin = dict(Celsius=celsius_from_kelvin, Fahrenheit=fahrenheit_from_kelvin)
to_kelvin = dict(Celsius=celsius_to_kelvin, Fahrenheit=fahrenheit_to_kelvin)
定义其他"{scale}_to_kelvin()"
和"{scale}_from_kelvin()"
函数,并将它们添加到字典中。
如果定义了所有转换器功能;你可以自动生成词典:
kelvin_to_kelvin = kelvin_from_kelvin = lambda temperature: temperature
to_kelvin = {scale: globals()[scale.lower() + "_to_kelvin"]
for scale in temperature_scales}
from_kelvin = {scale: globals()[scale.lower() + "_from_kelvin"]
for scale in temperature_scales}
或者您可以使用lambda
手动内联手动提供转换器功能:
from_kelvin = {
"Celsius": lambda t: t - 273.15,
"Fahrenheit": lambda t: (t * 9 / 5.) - 459.67
}
如果定义了C_to_F()
和C_to_kelvin()
,则无需定义F_from_kelvin()
功能。它允许定义更少的转换函数:如果有N个温度标度,那么您只需要定义2*N
转换器函数(从/到)而不是N*N
函数,即在这种情况下16而不是64。
GUI(或ncurses)接口会更方便。