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!
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:
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.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.