Python Forum
Function will not return variable that I think is defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function will not return variable that I think is defined
#1
I am teaching myself python and have this function in a programme:

x1 = 1
x2 = 2
y1 = 4
y2 = 6

def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    return 0.0
print(dx, dy)
the output is:
Output:
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> = RESTART: C:\Users\John\Documents\John''s files\Work\Coding\Think Like a Computer Scientist\Functions\distance_calculation.py Traceback (most recent call last): File "C:\Users\John\Documents\John''s files\Work\Coding\Think Like a Computer Scientist\Functions\distance_calculation.py", line 10, in <module> print(dx, dy) NameError: name 'dx' is not defined >>>
I think I have defined dx at dx = x2 - x1 but clearly the Interpreter thinks not! Any help will be gratefully received
Reply
#2
You may be looking at a scope problem.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
dx and dy are defined within local scope of the distance function. They values are not available in the outer scope (outside of that function). Once the function is defined you need to invoke it. Complete working example would be as follows:
 
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    return dx, dy

dx, dy = distance(1, 4, 2, 6)
print(dx, dy)
Reply
#4
Hello scidam

Thank you for your interest and suggestion. I can understand the logic of your code and it gives the right answer and I have learnt a little more about Python. However, I want to develop the programme to prompt for user input for the 'x' and 'y values so that the function receives input and then executes the computation. This is the reason I started off with a list of 'x' and 'y' values: now I have tried to insert the 'x' and 'y' value list within the function but it does not compute. Are you able to shed any light on this?

Thanks again for your help

Hello Paul

Thanks for your input. Being a complete Python Tyro I don't understand what you mean by 'scope problem'. I have had input from scidam, which has extended my limited knowledge but I am still struggling to gain full insight.

Thanks again.
Reply
#5
If you don't know about scope you should stop programming and start doing some reading. Scope is critical to writing python programs. More critical than knowing how to write a for loop or a print statement.
Reply
#6
Hello Deanhystad
Thanks for your direct advice - very wise words I have found. Apologies for taking so long to get back but I have been reading and now understand the point being made about Scope. I had missed this completely in my learning so far so you advice was timely and well appreciated.
Reply
#7
If you don't understand scope many Python errors are completely mystifying. I set x = 10, why is it 5 here? What do you mean name "y" is not defined? I set it right here!! But when you know about scope the error messages not only tell you what you did wrong, but where. You'll still smack your head a few times, but you are probably past spending hours trying to fix a scope error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to invoke a function with return statement in list comprehension? maiya 4 706 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 1,500 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 1,301 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
Sad Function defined by branches antoniogalante 1 628 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  Return not exiting function?? rudihammad 3 1,357 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 868 Nov-07-2020, 08:59 AM
Last Post: buran
  Why does my function return None? vg100h 3 891 Oct-29-2020, 06:17 AM
Last Post: vg100h
  function call at defined system time? Holon 5 1,353 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 689 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  how to keep a Popen instance existant in a function return? Skaperen 7 1,283 Sep-17-2020, 07:10 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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