温度转换

时间:2014-12-06 21:56:09

标签: python

我正在尝试编写一个python函数,在celsius和farenheit之间进行转换,然后是一个程序,它首先提示输入临时标度(c或f),然后是临时值,然后转换为另一个。到目前为止我所拥有的:

def convert_temp( source_temp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5/0))+32

source_temp = int(input)'Key in temp:'))
scale = input('(c) or (f)?')
y = conv(source_temp,scale)
print(tmp, 'in ',scale,"='s",y)

然而,当我尝试运行该程序时,我收到了很多回溯和语法错误。我做错了什么?

5 个答案:

答案 0 :(得分:2)

替换这个:

9.0/5/0        # you will get ZeroDivisionError

为:

9.0/5.0

替换这个:

source_temp = int(input)'Key in temp:')) # there should be opening bracket after input

为:

source_temp = int(input('Key in temp:'))

替换这个:

y = conv(source_temp,scale)

为:

y = conv_temp(source_temp,scale)

更改ur print语句:

print(source_tmp, 'in ',scale,"='s",y)       # here tmp was not defined, its source_tmp

答案 1 :(得分:2)

您的代码中存在许多问题。

def convert_temp( source_temp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5/0))+32

首先,tmp在此范围内未定义。您的参数名为source_temp,而不是tmp。更改函数定义将修复该错误。此外,您在其中一个表达式中输入了拼写错误,并用斜杠替换了一个点。此功能将正常工作:

def convert_temp( tmp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5.0))+32

接下来,您在程序正文中出现了一些语法错误:

source_temp = int(input)'Key in temp:'))

此行中有不匹配的括号。它应该是

source_temp = int(input('Key in temp:'))

再向下:

y = conv(source_temp,scale)

conv()不是一个功能。相反,您应该使用您定义的convert_temp()函数

y = convert_temp(source_temp,scale)

最后,

print(tmp, 'in ',scale,"='s",y)

tmp现在尚未定义。使用已定义的source_temp变量,如下所示:

print(source_temp, ' in ',scale," ='s ",y)

答案 2 :(得分:0)

本声明中的不平衡括号至少是问题的一部分:

source_temp = int(input)'Key in temp:'))

请改为尝试:

source_temp = int(input('Key in temp:'))

另外:conv()convert_temp()raw_input()而非input(),除零等不同。

答案 3 :(得分:0)

其他响应对于代码的语法错误是很好的,但我可能还建议只有一个return语句。在大多数编程语言中,通常认为它有不止一次返回的不良做法。同样以下列方式执行此操作可防止在运行期间出现不当行为,即如果您传递“c”或“f”之外的字符会发生什么。这是一个有效的用例,特别是因为传递的值来自用户输入,并且没有事先检查非法值。

def convert_temp(source_temp, scale):
    final_temp = 0
    if scale == 'c':
        final_temp = (source_temp - 32.0) * (5.0 / 9.0)
    elif scale == 'f':
        final_temp = (source_temp * (9.0 / 5.0)) + 32
    else:
        raise Exception('Bad Temperature Scale')
    return final_temp

temp_input = int(input('Key in temp:'))
scale_input = input('(c) or (f)?')
y = convert_temp(temp_input, scale_input)
print(tmp, 'in ', scale,"='s", y)

基于所犯的错误,我想知道是否需要对这里的实际机制进行进一步的解释。由于您最初在函数调用和参数上不匹配。我怀疑你了解变量是什么:引用一个值的符号。 convert_temp函数中的source_temp和scale是函数本地的变量,它们不存在于函数外部,但它们在函数内部存在。这个概念被称为“范围”。调用该函数时,这些局部变量将获得调用中参数的值:

<return_variable> = convert_temp(<temp_arg>, <scale_arg>)

这导致了我的下一个观点:由于convert_temp函数是全局的,因此对于用户输入使用与函数中定义的参数相同的变量名称可能不是最好的。虽然不一定是错的,但它更加谨慎。

对于函数名称本身convert_temp,这是您调用函数的名称。与变量引用值的方式类似,函数是对行为的引用。因此,当您调用该函数时,您需要使用与其定义的相同的符号。

答案 4 :(得分:0)

尝试或改编这个更通用的功能:

C, F, K, R = c, f, k, r = ('C', 'F', 'K', 'R')
SCALES = {C: (0.0, 100.0), F: (32.0, 212.0), K: (273.15, 373.15), R: (491.67, 671.67)}

def convert(temperature, to_scale):
    value, from_scale = temperature
    from_scale_freeze, from_scale_boil = SCALES[from_scale]
    to_scale_freeze, to_scale_boil = SCALES[to_scale]
    from_scale_divisions = from_scale_boil - from_scale_freeze
    to_scale_divisions = to_scale_boil - to_scale_freeze
    return ((value-from_scale_freeze)*(to_scale_divisions/from_scale_divisions)+to_scale_freeze,
             to_scale)

my_favourite_temperature = (22, c)

print(convert(my_favourite_temperature, k))
# (295.15, 'K')
相关问题