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?

6
  • Does this answer your question? Python: Passing variables between functions Commented Jan 13, 2021 at 12:16
  • As a side note, there is no really need for the if/else. You can just do: print(abs(temp1 - temp2)) Commented Jan 13, 2021 at 12:18
  • I'm not sure @Tomerikoo. Thanks for the side note. I've edited my question. Commented Jan 13, 2021 at 12:34
  • To get useful answers please post a minimal reproducible example. In this case, we don't see how you call those functions, just their definition so it's hard to help Commented Jan 13, 2021 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. Commented Jan 13, 2021 at 12:50

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

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
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.
Global variables are generally frowned upon, because they can affect how a module behaves when it is run.
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...
@Techguy Don't make them global. It's possible, but not recommended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.