【问题标题】:Beginning Python functions and lists开始 Python 函数和列表
【发布时间】:2015-08-27 03:52:50
【问题描述】:

*快速编辑我使用的是 python 3.4

我刚开始在课堂上学习 python,虽然它看起来比 C++ 更容易处理,但有些东西我还没有解决。

我需要做一个小函数,它接受一些以华氏温度为单位的温度,并将它们转换为摄氏度,并将它们放回列表形式。我有大部分代码,我相信它应该可以工作。但是,我不断收到一个变量尚未声明的错误。

input_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]

def farenheit_to_celsius(data):         #function definition
    output_temps = []                   #create empty list
        for input in data:              #cycle through list
            ((input_temps-32)* 5/9)     #done to each item oninput_temps
    output_temps += [input]             #connects list to list
    return output_temps                 #returns output_temps with newvalues

 print(output_temps)                    #prints out output_temps to debug

代码在我的编辑器上正确缩进。但是,当我尝试运行它时,它说“output_temps”尚未声明。另外,我正在尝试将列表添加到列表中,但我不确定我是否正确。

output_temps += [input]          #not 100% sure if this is right.

【问题讨论】:

  • 这里的缩进肯定看起来不正确,例如 print 语句已关闭。
  • 我遇到了逻辑问题,因为这是我第一次尝试弄清楚如何将代码粘贴到网站中,所以缩进已关闭。

标签: python list python-3.4


【解决方案1】:

首先,如果您不重新分配 input 变量,则它们不会被修改。其次,output_temps 中的修改应该在循环内完成。第三也是最重要的,函数应该已经被调用了(output_temps 的作用域在函数执行结束后结束)。

input_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]
def farenheit_to_celsius(data):         #function definition
    output_temps = []                   #create empty list
    for input in data:              #cycle through list
        input = ((input-32)* 5/9)     #done to each item oninput_temps
        output_temps += [input]             #connects list to list
    return output_temps                 #returns output_temps with newvalues

print(farenheit_to_celsius(input_temps))

output_temps += [input] 是正确的。它将 input 放在一个列表中,它是唯一的元素,然后将其添加到 output_temps 。但是,可能更好的方法是使用append method on lists

output_temps.append(input)

【讨论】:

  • 非常感谢。粘贴时,我确实以某种方式删除了我的“ input = ((input-32)* 5/9) ”。我将打印语句设置为“ print(output_temps)”的原因是因为我们的讲师就是这样调用它的。 # 打印结果 print(output_temps)
【解决方案2】:
for input in data:              
    ((input_temps-32)* 5/9) # this should be input = (input - 32)*5/9

Int 在 Python 中是不可变的。
您的代码中有一些缩进错误。
而且您在打印 output_temps 之前没有调用 farenheit_to_celsius。

input_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]

def farenheit_to_celsius(data):         #function definition
    output_temps = []                   #create empty list
    for input in data:              #cycle through list
        input =  ((input-32)* 5/9)     #done to each item oninput_temps
        output_temps += [input]             #connects list to list
        # I think use output_temps.append(input) will be better. But it's ok here.
    return output_temps                 #returns output_temps with newvalues

print(farenheit_to_celsius(input_temps))                    #prints out output_temps to debug

【讨论】:

  • 我认为 int 列表可能是可变的。只有元组是不可变的。谢谢你的休息。传递函数参数有点生疏
  • @Luis List 是可变的,但 Int 不是。所以((input_temps-32)* 5/9) 只是生成一个数字而不改变 input_temps 本身
【解决方案3】:
input_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]


def farenheit_to_celsius(data):
    output_temps = []

    for input in data:
        ((input_temps-32)* 5/9)
        output_temps += [input]

    return output_temps
    print(output_temps)

你应该很好,我只需要修复这个程序中的一些空白错误,并且 output_temps += [input] 好的,它不会导致任何错误

【讨论】:

    【解决方案4】:

    output_temps 是在您的方法内部声明的,但在您的 print 语句中被引用,而该语句不在该方法内部,这就是您收到错误的原因。打印您对方法的调用,而不是打印 output_temps,这将返回 output_temps:

    print(farenheit_to_celsius(my_data))
    

    要在 Python 中添加到列表中,您可以这样做:

    my_list.append(my_data)
    

    【讨论】:

    • 不!不要在函数之外声明它;您需要调用函数,该函数返回 output_temps.
    • 哎呀,你是对的,那会好很多。 @Cyphase
    【解决方案5】:

    问题是您定义函数,而不是调用函数。就像在 C++ 中一样,除非您调用该函数,否则函数中的任何代码都不会运行。

    此外,即使您在 print 之前调用该函数,print 也不起作用,因为您试图从函数外部访问函数内部的变量 output_temps。该函数返回output_temps,所以你应该这样做:

    output_temps = fahrenheit_to_celsius(input_temps)
    

    您在函数本身内部也有问题,您还没有遇到过,因为您没有在代码中调用它。首先,((input_temps-32)* 5/9) 指的是温度列表,而不是您正在迭代的当前温度。但即使你这样做((input-32)* 5/9),它只会计算你想要的值,它不会保存它。你需要对结果做点什么;例如,您可以将其分配给一个变量:

    result = (input_temps - 32) * (5 / 9)
    

    接下来,output_temps += [result] 应该for 循环中;否则,您只需在 for 循环完成后将最后一个温度添加到输出列表中。此外,您应该使用output_temps.append() 向列表中添加一个值,而不是您现在正在执行的操作。

    这是您的代码的固定版本,对变量名称进行了一些更改:

    fahrenheit_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]
    
    def fahrenheit_to_celsius(temps):
        celsius_temps = []
        for fahrenheit in data:
            celsius = (input_temps - 32) * (5 / 9)
            celsius_temps.append(celsius)
        return celsius_temps
    
     print(fahrenheit_to_celsius(fahrenheit_temps))
    

    现在,一般来说,我建议让函数只处理一个值,并在函数之外处理列表。

    这段代码最终与上面的代码做同样的事情,但它更简洁、更灵活。

    def fahrenheit_to_celsius(temp):
        return (temp - 32) * (5 / 9)
    
    fahrenheit_temps = [-20.0, -10.0, 5.0, 18.0, 50.0, 212.0]
    celsius_temps = [fahrenheit_to_celsius(temp) for temp in fahrenheit_temps]
    
    print(celsius_temps)
    

    【讨论】:

    • 是的,我以某种方式设法从 input = "((input-32)* 5/9)" 中删除了 "input = " 这就是他编写代码的方式,只是为了让我们的脚湿透,在Python。感谢您的输入。真的很喜欢看到不同的做事方式。
    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2016-04-10
    • 2015-04-22
    相关资源
    最近更新 更多