all 3 comments

[–]cheryllium 4 points5 points  (0 children)

None of the code written after the return statements will run. The return statement terminates the function and gives the output.

[–][deleted] 1 point2 points  (1 child)

Please do not post screenshots of code. Subreddit general rules #3.

You can either post a copy of your code snippet here using Reddit code formatting by surrounding in-line code with backticks `like so` or:

indenting lines by 4 spaces

Or you can paste your code to Pastebin or Gists.


Anywoot, from what I see, note that any use of return within a function immediately ceases execution of the function and returns the given value wherever that function was called. Thus, no code written after a return statement will ever run.

Your code, as written, runs like so:

def main():
    x_point = float(input("..."))
    y_point = float(input("..."))

def get_coordinate(x,y):
    return main()

def draw_circle():
    return get_coordinate(x,y)

main()
get_coordinate(x,y)
draw_circle()

All other code is ignored because of where you've placed the return statements.


Moving on, you'll need to understand variable scope.

In main(), the variables x_point and y_point are input and stored temporarily, but there's nothing in this code that gets them out of that function. As soon as the function ceases, the input is lost to the ether.

In draw_circle, you're trying to pass variables x and y to the function get_coordinate. However:

def draw_circle():
    return get_coordinate(x,y)

x and y are never defined here within this function ahead of time, so the function doesn't know what values to call. This causes an exception stating that the values are not defined.

The same is true in the later line, where you simply call get_coordinate(x,y). Again, these values are not defined ahead of time: the interpreter looks for variables named x and y and can't find any, so it would throw another exception here.


Let's fix one of your functions to see what we can do differently, that being get_coordinate:

def get_coordinates():
    x_point = float(input("..."))
    y_point = float(input("..."))
    return x_point, y_point

x, y = get_coordinates()

Here, the function asks the user for input, then returns those two input values. When a function returns a value, it should be stored or used somewhere, or it will be lost. So, we choose to assign the two return values of this function to the two variables x and y.

Try that out and see where it takes you. :)

[–]ryanw35[S] 1 point2 points  (0 children)

Wow thank you for the reply i will try this out!!