我开始学习python,这是我的第一个项目,但我并不完全高兴,我认为它可以做得更好。你能帮我一下吗?这些字符串是西班牙语的,但我认为这不会损害理解。
elegir_magnitud = int(input(
"""1 = Temperatura
2 = Longitud
3 = Masa
4 = Volumen
5 = Tiempo
6 = Divisa
Entra número deseado: """))
if elegir_magnitud == 1 :
unidad_temperatura = int(input(
"""
------------------Elige la unidad de temperatura a ser convertida:---------------
1 = Celsius
2 = Farhengei o como se escriba
3 = Kelvin
Entra número deseado: """))
cantidad_temperatura = float(input(
"""
Entra la cántidad de esa unidad deseada: """))
#ELEGIR UNIDAD DE TEMPERATURA A CONVERTIR
unidad_a_convertir = int(input(
"""
--------------------Elige la unidad de temperatura a convertir:-----------------
1 = Celsius
2 = Farhengei o como se escriba
3 = Kelvin
Entra número deseado: """))
if unidad_temperatura == unidad_a_convertir:
print("Por qué quieres convertir a la misma unidad?")
if unidad_temperatura == 1 and unidad_a_convertir == 3:
print("""El resultado de la conversion es: \n""",float(cantidad_temperatura - 273),"ºK")
if unidad_temperatura == 1 and unidad_a_convertir == 2:
print("""El resultado de la conversion es: \n""",float(cantidad_temperatura * 9%5 +32),"ºF")
if unidad_temperatura == 3 and unidad_a_convertir == 1:
print("""El resultado de la conversion es: \n""",float(cantidad_temperatura + 273),"ºC")
if unidad_temperatura == 3 and unidad_a_convertir == 2:
print("""El resultado de la conversion es: \n""",float((cantidad_temperatura -273) * 9 % 5 + 32),"ºF")
if unidad_temperatura == 2 and unidad_a_convertir == 3:
print("""El resultado de la conversion es: \n""",float((cantidad_temperatura - 32) % 9 * 5 - 273),"ºK")
if unidad_temperatura == 2 and unidad_a_convertir == 1:
print("""El resultado de la conversion es: \n""",float(cantidad_temperatura % 9*5 -32),"ºC")发布于 2020-07-13 17:54:51
这些字符串是西班牙语的。
太棒了!然而,这并不是西班牙语中唯一的东西:变量(elegir_magnitud、unidad_temperatura等)也一样。
这是不理想的,至少有几个原因:
将国际化(i18n)应用于您的字符串是一件很棒的事情,但它应该只适用于您的字符串,而不是代码。
考虑将代码重构为:
你确定这样能行吗?
cantidad_temperatura % 9*5 -32我怀疑模数在这里是正确的。你应该使用组织/。
备注:
locale方法的使用,而不是直接使用int和float构造和格式化。from locale import getlocale, getdefaultlocale
# For testing only
# from locale import setlocale, LC_ALL
# setlocale(LC_ALL, 'es_ES.UTF8')
def get_lang() -> str:
lang, _ = getlocale()
def_lang, _ = getdefaultlocale()
return (lang or def_lang)[:2]
LANG = get_lang()
if LANG == 'en':
DIMENSIONS = (
'Temperature',
'Length',
'Mass',
'Volume',
'Time',
'Currency',
)
TEMP_UNITS = (
'Kelvin',
'Celsius',
'Fahrenheit',
)
ENTER_NUMBER = 'Enter the desired number: '
CHOOSE_TEMP_SOURCE = 'Choose the source temperature unit to convert: '
CHOOSE_TEMP_DEST = 'Choose the destination temperature unit to convert: '
PROVIDE_TEMP = 'Provide the source temperature: '
RESULT = 'The result of the conversion is:'
elif LANG == 'es':
DIMENSIONS = (
'Temperatura',
'Longitud',
'Masa',
'Volumen',
'Tiempo',
'Divisa'
)
TEMP_UNITS = (
'Kelvin',
'Centígrada',
'Fahrenheit'
)
ENTER_NUMBER = 'Entra número deseado: '
CHOOSE_TEMP_SOURCE = 'Elija la unidad de temperatura fuente para convertir: '
CHOOSE_TEMP_DEST = 'Elija la unidad de temperatura de destino para convertir: '
PROVIDE_TEMP = 'Proporcionar la temperatura de la fuente: '
RESULT = 'El resultado de la conversión es:'from enum import Enum
from locale import format_string, atof, atoi
from typing import TypeVar, Type, Iterable
from strings import *
class Dimension(Enum):
TEMPERATURE = 1
LONGITUDE = 2
MASS = 3
VOLUME = 4
TIME = 5
CURRENCY = 6
class TempUnit(Enum):
KELVIN = 1
CELSIUS = 2
FAHRENHEIT = 3
# To get from this unit to Kelvin:
# kelvin = m * this_unit + b
TEMP_PARAMETERS = {
# This unit m b
TempUnit.KELVIN: (1, 0),
TempUnit.CELSIUS: (1, 273.16),
TempUnit.FAHRENHEIT: (5/9, 273.16 - 5/9*32),
}
InputEnum = TypeVar('InputEnum', bound=Enum)
def enum_from_input(t_enum: Type[InputEnum], labels: Iterable[str]) -> InputEnum:
print(
'\n'.join(
format_string('%d = %s', (enum.value, label))
for enum, label in zip(t_enum, labels)
)
)
result = t_enum(atoi(input(ENTER_NUMBER)))
print()
return result
def main():
dimension = enum_from_input(Dimension, DIMENSIONS)
if dimension != Dimension.TEMPERATURE:
raise NotImplementedError()
print(CHOOSE_TEMP_SOURCE)
source = enum_from_input(TempUnit, TEMP_UNITS)
print(CHOOSE_TEMP_DEST)
dest = enum_from_input(TempUnit, TEMP_UNITS)
temp1 = atof(input(PROVIDE_TEMP))
# Use a simple linear transformation where y = mx + b; first convert to
# the base unit (Kelvin)
m, b = TEMP_PARAMETERS[source]
kelvin = m*temp1 + b
# Now calculate inverse to the destination unit
m, b = TEMP_PARAMETERS[dest]
temp2 = (kelvin - b)/m
print(format_string('%s %.1f', (RESULT, temp2)))
if __name__ == '__main__':
main()1 = Temperatura
2 = Longitud
3 = Masa
4 = Volumen
5 = Tiempo
6 = Divisa
Entra número deseado: 1
Elija la unidad de temperatura fuente para convertir:
1 = Kelvin
2 = Centígrada
3 = Fahrenheit
Entra número deseado: 3
Elija la unidad de temperatura de destino para convertir:
1 = Kelvin
2 = Centígrada
3 = Fahrenheit
Entra número deseado: 2
Proporcionar la temperatura de la fuente: 81
El resultado de la conversión es: 27,2https://codereview.stackexchange.com/questions/245418
复制相似问题