-3

Sorry I couldn't find a better formulation of what I mean't, So basically I'm trying to create a script that auto-complete a math problem, but now I'm stuck and couldn't find a good answer on forums.

Here how it looks

print("That's how you type the function : f(x) = a*sin(b(x-h))+k\nDon't use space!")
print("If the value of the variable is unknown type 'None'")
import math
def start():
    y= input("y = ")
    sct = input("sin, cos, tan = ")
    a= input("a = ")
    b= input("b = ")
    x= input("x = ")
    h= input("h = ")
    k= input("k = ")
    print("So if I understand here the problem you want to solve")
    print(y,"=",a,sct,"(",b,"(",x, "-" ,h,"))",k)
    QUA = input("Yes or No? : ")
    if QUA == "Yes":
            print("Good")
    elif QUA == "No":
        start()
start()

So the part where I'm stuck at is that I'm asking a question and if you say "Yes" it continues the script. If you say "No" it goes back to start(). So when I try after that to use the variables it just says

>>> print(y)
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    print(y)
NameError: name 'y' is not defined

Help anyone?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Voxaim
  • 45
  • 4
  • Local variables are *local* -- they're only defined while the function is still running. Maybe you should make a class, instead of a function, so you can give it instance variables? – Charles Duffy Mar 15 '18 at 22:07
  • 1
    This is definitely a dup; it's come up three times in the last week. (It didn't used to come up this often… Wonder what's changed?) Anyone know where the canonical answer is? – abarnert Mar 15 '18 at 22:07
  • https://stackoverflow.com/questions/1360721/how-to-get-set-local-variables-of-a-function-from-outside-in-python is the oldest one I can find, but it's not a very good question, and the answers all suggest different things besides the obvious "return the value(s)". – abarnert Mar 15 '18 at 22:08
  • https://stackoverflow.com/questions/39689434/how-to-access-local-variables-of-functions-when-the-function-call-has-finished-i isn't bad, but as it has -1 votes and the answer has 0, I'm guessing it's not the canonical question… – abarnert Mar 15 '18 at 22:11
  • Hum... would that one work? https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement – Olivier Melançon Mar 15 '18 at 22:12
  • @OlivierMelançon: I think not -- we need a question written from the viewpoint of wanting to deal with internal variables. Your link goes straight to the solution; not obvious enough for my taste. – Prune Mar 15 '18 at 22:13
  • @OlivierMelançon Not sure. You and I can see how it answers the OP's question, but could the OP, or someone else searching on the same problem, understand that without any additional help? – abarnert Mar 15 '18 at 22:14
  • I believe this is close then: https://stackoverflow.com/questions/14051916/python-how-to-make-a-local-variable-inside-a-function-global Or its own duplicate, but I prefer the linked post answer as it recommend returning – Olivier Melançon Mar 15 '18 at 22:15
  • After looking at three pages of results, Tim Pietzcker's answer to that question seemed concise an understandable, and the question wasn't full of irrelevant and distracting details. If anyone comes up with a better dup, change it. – abarnert Mar 15 '18 at 22:23

1 Answers1

0

You return it, otherwise it is not accessible outside the scope of your function.

def start():
    y = input("y = ")
    return y

y = start()
print(y)

If you need more than one returned value, you can return more than one.

def start():
    x = input("x = ")
    y = input("y = ")
    return x, y

x, y = start()
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73