将列表作为输入传递

2024-09-30 13:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的摄氏到华氏的代码。我在创建要输出的10天列表时遇到问题。我可以输入一个数据,它会给我华氏温度,但我不知道如何输入一个10天的列表,输出计算结果,以及它是冷还是热

tmp = int(input("Input the  temperature you like to convert from Celsius to Fahrenheit? (e.g.,10) : "))
lst = [tmp]
i = 0
for i in range(len(lst)):
    lst[i] = (lst[i] * 1.8) + 32
    print(lst, "This is the fahrenheit temps")

if tmp < 0:
    print('Freezing weather.\n')
elif tmp < 10:
    print('Very cold weather.\n')
elif tmp < 20:
    print('Cool weather\n')
elif tmp < 30:
    print('Normal temps.\n')
elif tmp < 40:
    print("It's hot outside.\n")
else:
    print("You should properly stay inside")

Tags: theto数据代码列表inputtmpint
2条回答

好吧,我根据你的需要重新编写了你的代码

temps = map(float, input("Input the temperature you'd like to convert from Celsius to Fahrenheit: ").split(',')) #split it with comma (,) and then convert every item into float
for temp in temps: #loop through the values
    fahrenite = temp * 9/5 + 32 #calculate
#blah blah
    if temp< 0:
        extra = "Freezing weather."
    elif temp< 10:
        extra = "Very cold weather."
    elif temp< 20:
        extra = "Cool weather"
    elif temp< 30:
        extra = "Normal temps."
    elif temp< 40:
        extra = "It's hot outside."
    else:
        extra = "You should properly stay inside"
#print everything
    print(f"{temp}℃  in Fahrenheit is {fahrenite}℉  that is {extra}")

还有一件事……用逗号(,)来分隔你的输入

一种方法是将不同的temp分离到不同的输入变量中,并将它们以实物形式附加到列表中,然后使用函数遍历该列表

像这样:

inputting = True
temps = []
while inputting:
    temp = int(input("Temp"))
    temps.append(temp)
    cond = input("continue? y/n")
    if cond in "Nn":
        inputting = False

def c_to_f(temperature):
    return temperature * 1.8 + 32

for temp in temps:
    print(c_to_f(temp))

相关问题 更多 >

    热门问题