Report this

What is the reason for this report?

function error removal

Posted on June 4, 2021
def my_func():
	input_pressure = float(input("Enter pressure in psi: "))
	print("Value inside function:",input_pressure)
my_func()
# conv_press = input_pressure/14.6;
print("Value outside function:",+input_pressure/14.6)


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

Usually, variables defined inside a specific function are not accessible outside of that function.

This should work with the following syntax by attaching the variable to an object so it could be accessible outside of the function:

def my_func():
    my_func.input_pressure = float(input("Enter pressure in psi: "))
    print("Value inside function:",my_func.input_pressure)
my_func()

print("Value outside function:",+my_func.input_pressure/14.6)

Hope that this helps. Regards, Bobby

The error encountered in the code is due to the scope of the input_pressure variable. It is defined within the my_func() function, making it a local variable only accessible within that function. When you try to access it outside of my_func(), it’s not recognized, which leads to a NameError.

To fix this issue, you can modify your code in a couple of ways:

Option 1: Return the Value from the Function

You can make my_func() return the input_pressure value and then use it outside the function.

def my_func():
    input_pressure = float(input("Enter pressure in psi: "))
    print("Value inside function:", input_pressure)
    return input_pressure

input_pressure = my_func()
conv_press = input_pressure / 14.6
print("Value outside function:", conv_press)

This way, input_pressure is returned from the function and stored in a variable of the same name outside of the function, allowing you to use it subsequently to calculate conv_press.

Option 2: Global Variable

If input_pressure must be used across different parts of your script, you could declare it as a global variable, although this is generally less recommended due to potential issues with code maintainability and debugging.

input_pressure = 0  # Declare the variable globally

def my_func():
    global input_pressure
    input_pressure = float(input("Enter pressure in psi: "))
    print("Value inside function:", input_pressure)

my_func()
conv_press = input_pressure / 14.6
print("Value outside function:", conv_press)

With this method, you declare input_pressure as a global variable outside the function. Inside my_func(), you tell Python that you intend to use the global version of this variable with the global keyword.

The first option (returning the value from the function) is generally better practice in programming because it avoids side effects that can occur with global variables, making your code easier to understand and maintain.

Choose the method that best suits the overall design of your application.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.