Home > Back-end >  passing a list as input
passing a list as input

Time:10-06

I have this simple Celsius to Fahrenheit code. I am having trouble creating a list of 10 days to be output. I can put one input and it give me Fahrenheit temperature but I'm blanking out on how to input a list of the 10 days to be output the calculation and if its cool or warm.

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")

CodePudding user response:

Okay, so I remade your code as per your needs

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}")

And one more thing......use comma (,) to seperate your inputs

CodePudding user response:

One way to do it is to separate different temps into different input variables, and append them to a list in kind, then iterate through the list with your function.

Like so:

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))

CodePudding user response:

This is an alternative solution:)


    def checkInt(str):
        if str[0] in ('-', ' '):
            return str[1:].isdigit()
        return str.isdigit()


    tmp = 'start'
    lst = []
    while True:
        inp = input("Input the temperature you like to convert from Celsius to Fahrenheit?\n input any string to stop ")
        if checkInt(inp):
            lst.append(int(inp))
        else:
            break

    farn = [i * 1.8   32 for i in lst]
    print(f"This is the fahrenheit temps {farn}")

    dc = {0: 'Freezing weather.\n',
          10: 'Very cold weather.\n',
          20: 'Cool weather\n',
          30: 'Normal temps.\n',
          40: "It's hot outside.\n"}

    for temp in farn:
        a = [temp < i for i in dc.keys()]
        if True in a:
            print(dc[a.index(True)*10])
        else:
            print("You should properly stay inside")


  • Related