0

I have two functions:

def read_temp():
    lines1, lines2 = read_temp_raw()
    while lines1[0].strip()[-3:] != "YES":
        time.sleep(0.2)
        lines1, lines2 = read_temp_raw()
    temp1 = calculate_temp(lines1)
    temp2 = calculate_temp(lines2)
    return temp1, temp2

def Temp_difference():
    if temp1 > temp2:
        print(temp1 - temp2)
    else:
        print(temp2 - temp1)

I want to use temp1 and temp2 in Temp_difference. When I try adding the variables globally in read_temp, my IDE (Pycharm) says: "Global variable 'temp1/2' is undefined at the module level". I've tried to do it this way:

def Temp_difference(temp1, temp2):
    print (abs(temp1-temp2))

I'm not getting any warnings or errors here, but I don't know if that is correct. So is there a better/correct way to do this?

Techguy
  • 17
  • 5
  • Does this answer your question? [Python: Passing variables between functions](https://stackoverflow.com/questions/16043797/python-passing-variables-between-functions) – Tomerikoo Jan 13 '21 at 12:16
  • As a side note, there is no really need for the `if/else`. You can just do: `print(abs(temp1 - temp2))` – Tomerikoo Jan 13 '21 at 12:18
  • I'm not sure @Tomerikoo. Thanks for the side note. I've edited my question. – Techguy Jan 13 '21 at 12:34
  • To get useful answers please post a [mre]. In this case, we don't see how you call those functions, just their definition so it's hard to help – Tomerikoo Jan 13 '21 at 12:41
  • I'm not at that point yet. It's part of a project involving temperature sensors with Raspberry Pi. I haven't got a chance to test it yet, but I'm thinking of just `Temp_difference()`. It's about water leak detection. – Techguy Jan 13 '21 at 12:50
  • When you feel that your functions need variables made by each other, and it is getting tedious to pass around variables, It is a good indication to start using Classes. :) – Abdul Aziz Barkat Jan 13 '21 at 14:37

2 Answers2

2

In your Temp_difference() function, you can call your read_temp() function. Then you can save the returned values as the temp1 and temp2 variables. Here is the code:

def Temp_difference():
    temp1, temp2 = read_temp()
    if temp1 > temp2:
        print(temp1 - temp2)
    else:
        print(temp2 - temp1)
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
-1

As you have it, your variables are local to the given functions. You can use a global variable inside other functions by declaring it as global within each one of the functions that assigns a value to it:

MF-
  • 185
  • 8
  • 1
    There is no real reason to use `global` here. Notice that `read_temp` already returns the values. All that's missing is to pass them to `Temp_difference` – Tomerikoo Jan 13 '21 at 12:20
  • I agree, however if he's keeping the variable name the same it makes it easier to follow. It also eliminates the need to pass them. – MF- Jan 13 '21 at 12:24
  • Global variables are generally frowned upon, because they can affect how a module behaves when it is run. – DapperDuck Jan 13 '21 at 12:25
  • 1
    *It also eliminates the need to pass them* - but adds the need to make them `global` with all other implications of that. So what is the gain? You can still keep the names exactly the same, because of the reason that they are local... – Tomerikoo Jan 13 '21 at 12:26
  • When i try to make them `global` Pycharm comes with the message that i described in the question. – Techguy Jan 13 '21 at 12:43
  • 1
    @Techguy Don't make them global. It's *possible*, but not recommended. – chepner Jan 13 '21 at 12:45