-1

I am a beginner to python and coding in general and have not a clue why return in the code below doesn't move across functions. Shouldn't the value for n be stored for use in all functions? print(n) is in there only for my own sake to see if it works, and it is apparently not.

def main():
    print("This program tests the Goldbach's conjecture")
    get_input()
    print(n)


def get_input():
    n = 0
    while n == 0:
        try:
            v = int(input("Please enter an even integer larger than 2: "))
            if v <= 2:
                print("Wrong input!")
                continue
            if v%2 == 1:
                print("Wrong input!")
                continue
            if v%2 == 0:
                n = v
        except ValueError:
            print("Bad input!")
            continue
    return n
NessP
  • 1
  • The marked duplicate is overkill. However, this scoping information is available from almost any tutorial on Python functions. Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Mar 26 '21 at 23:10

1 Answers1

1

You are not storing the value returned by get_input anywhere, you should keep it in a variable (or printing it directly), e.g -

def main():
    print("This program tests the Goldbach's conjecture")
    val = get_input()
    print(val)

n is an internal variable that stored only within the scope of get_input.

Hook
  • 355
  • 1
  • 7
  • oh ok so return only stores the variable in terms of the variable the function is being called to respond to. thanks! – NessP Mar 27 '21 at 00:09