【问题标题】:Need help getting a while loop to work with a list in python需要帮助获得一个while循环来处理python中的列表
【发布时间】:2019-09-28 03:27:06
【问题描述】:

我正在制作一个小程序,使用 while 循环将华氏温度转换为摄氏温度,方法是从列表中获取温度并将其发送到函数进行计算。一旦它将温度从 F 转换为 C,它将打印每个温度的结果。我不希望为我编写代码,我只是在寻找一些信息来让我朝着正确的方向前进。这是我到目前为止所拥有的,但我认为我不需要其中的 for 循环。 temp 应该是华氏温度,并且 add 函数应该将其转换为摄氏度。

def add(x):
    return float(x - 32) * 5.0/9.0


temp = [-10, -2, 7, 16, 24, 32, 41, 50, 58, 67, 75]
while True:
    for x in temp:

        print(x)

【问题讨论】:

    标签: python function while-loop temperature


    【解决方案1】:

    您不需要while True 循环。而且您不想打印temp 中的项目;您想在temp 中的每个项目上打印调用add()结果

    for x in temp:
        print(add(x))
    

    【讨论】:

    • 感谢您的帮助。我知道我在哪里犯了错误。我最终只是删除了 while 循环并更改了 print 语句,它现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2023-02-19
    相关资源
    最近更新 更多