Python Forum
name 'temperatures' not defined?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name 'temperatures' not defined?
#1
I get an error saying, print('The input list is:', temperatures)
NameError: name 'temperatures' is not defined

This is my code:

#initialise the input_list with the given values

#initialise the output_list to the empty list
def get_input():
    temperatures = get_input()
    input_list = list(
        map(float, input("\nEnter the temperatures:").strip().split()))
#if the input_value satisfies the condition:
    return input_list
    temperatures = get_input()
    output_list = []
    for i in temperatures:
        if i > 0.0 and i < 5.0:
            point = 0
# append the input_value to the output_list
    else:
            output_list.append(i)
# print the output_list
print('The input list is:', temperatures)
print('The output list is:', output_list)
How do I define Temperatures?
The table is,

Input Output
[4.7] []
[5.2] [5.2]
[0, 3, 5, 7, 5, 4, 2, 0, -0.2]. [7,-0.2]
Reply
#2
try this:
def get_input():
    input_list = list(
        map(float, input("\nEnter the temperatures:").strip().split()))
    return input_list



temperatures = get_input()
output_list = []
for i in temperatures:
    if i > 0.0 and i < 5.0:
        point = 0
    else:
        output_list.append(i)


print('The input list is:', temperatures)
print('The output list is:', output_list)
Reply
#3
You could do something like this:
import ast

def get_input():
    input_list = ast.literal_eval(input("\nEnter the temperatures: "))
    return input_list

def get_temperature(temps):
  output_list = []
  for t in temps:
    if t >= 0 and t <= 5:
      pass
    else:
      output_list.append(t)
  
  return output_list

input_list = get_input()
output_list = get_temperature(input_list)
print('The input list is: ', input_list)
print('The output list is: ', output_list)
The "ast" library is there so you can convert a string representation of a list like "[0, 4, 5]" into an actual list like [0, 4, 5]. It is also safer than eval().

The way you had yours set up would have caused recursion because you were calling the function from within itself.
Reply
#4
The concept of "scope" is very important in Python. Modules (files) define a scope. Functions define a scope. Classes define a scope. When you see the message "X is not defined" it is often a matter of scope.

In your example "temperatures" is defined inside the scope of function get_input(). The print statements at the bottom of the module are outside the get_input() function's scope and cannot see "temperatures".

You could fix this by defining "temperatures in the module scope.
temperatures = []  # defines temperatures in module scope
def get_input():
    global temperatures # Use temperatures from module scope
    temperatures =  map(float, input("\nEnter the temperatures:").strip().split()))

print(temperatures)
Globals work ok when used sparingly, but there is no way to tell where the global variable is modified other than reading the code. Ok for 1 variable in a 50 line program, but tedious for many variables in a 1000 line module.

To make code easier to understand/read/debug the relationship between variable and modifier should be direct. Some variables can be passed as arguments to a function that then modifies the variable. This only works for mutable types like lists and dictionaries, and not for immutable types like numbers and strings. This would works (somewhat).
def get_input(temperatures):
        temperatures = list(map(float, input("\nEnter the temperatures:").strip().split()))

temperatures = []
get_input(temperatures)
print(temperatures)
Modifying arguments inside a funtion is frowned upon, because as with global variables the only way to know if a variable gets modified is to read the code. At least you don't have to search the entire file.

The best way to associate modification of a variable with a function call is to make the variable the return value. In this example there is no doubt that the function "get_input" modifies "temperatures".
def get_input():
        return list(map(float, input("\nEnter the temperatures:").strip().split()))

temperatures = get_input()
print(temperatures)
And if you have more than one return value, that isn't a problem in Python.
def get_input(minTemp, maxTemp):
        alltemps = list(map(float, input("\nEnter the temperatures:").strip().split()))
        in_range = [x for x in alltemps if minTemp <= x <= maxTemp]
        return alltemps, in_range

temperatures, temperate = get_input(12, 20)
print(temperatures)
print(temperate)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,860 May-30-2020, 04:14 AM
Last Post: DT2000
  Read 2 temperatures in Real Time from Arduino linkxxx 0 1,921 Aug-29-2019, 11:27 AM
Last Post: linkxxx
  Real time graph of the temperatures and storage linkxxx 3 2,713 Aug-29-2019, 09:44 AM
Last Post: linkxxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020