Python: итерация условного оператора в цикле - PullRequest
0 голосов
/ 30 сентября 2018

Итак, у меня есть список значений с именем temp_data, и я импортировал две функции, которые я хочу использовать:

# Function which converts from fahr to celsius
def fahr_to_celsius(temp_fahrenheit):
    converted_temp = (temp_fahrenheit - 32)/ 1.8 # Now we have assigned the convertable number to the variable
    return converted_temp

def temp_classifier(temp_celsius): #Function name temp_classifier, parameter temp_celsius that we later use to define the temperature
    if temp_celsius <-2: #Classifies temperatures below -2 degrees (Celsius) and returns value 0
        return 0
    elif (temp_celsius >-2) and (temp_celsius <2): #Classifies temperatures equal or higher than -2 up to +2 degrees (Celsius) and returns value 1
        return 1
    elif (temp_celsius >=2) and (temp_celsius <15): #Classifies temperatures equal or higher than +2 up to +15 degrees (Celsius) and returns value 2
        return 2
    elif temp_celsius >15: #Classifies temperatures equal or higher than +15 degrees (Celsius) and returns value 3
        return 3

Первая функция работает хорошо, когда я перебираю значения temp_data и преобразовать их в градусы Цельсия с выводом temp_celsius.Вывод

for t in temp_data: #Takes the iteration data from temp_data
    temp_celsius=(fahr_to_celsius(t)) #Assigns output to variable
    print(temp_celsius)

вывод:

-7.222222222222222
-6.111111111111111
-6.111111111111111
-6.111111111111111
-5.0

и т. Д. (336 значений).

Но тогда, когда я захочу использовать эти переменные temp_celsius в другой функции temp_classifier Я получаю ошибку 'float' object is not iterable.

for t in temp_celsius: #Takes the iteration data from temp_data
    temp_class=(temp_classifier(t)) #Assigns output to variable
    print(temp_class)

Что я здесь не так делаю?Цель состоит в том, чтобы назначить выходы первой функции temp_celsius категориям другой функции temp_classifier.

1 Ответ

0 голосов
/ 30 сентября 2018

Похоже, что вы выполняете итерацию по плавающей запятой, а не по всплывающим подсказкам, сохраненным в итерируемом объекте, например в списке.значение:

temp_class = temp_classifier(t)
    print(temp_class)
...