-4

I am trying to get my head around python. This is a snippet of code where I want to have the user input an option in a function and then use that input in another function(s). This tells me 'myInput' is not defined.

def main():          
    myInput = input("Enter a number ")
#     This is the main function that will be the primary executuable function - the start of the program

    return(myInput)


if __name__ == '__main__':
    main()
#


print (myInput)

  • Did you read and understand https://docs.python.org/3/tutorial/controlflow.html#defining-functions? If not, you should do so. – mkrieger1 Nov 28 '19 at 13:48
  • 1
    Possible duplicate of [What is the purpose of the return statement?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement) – mkrieger1 Nov 28 '19 at 13:55

1 Answers1

2

Function main returns the user's input, so try this:

if __name__ == '__main__':
    myInput = main()
goodvibration
  • 5,980
  • 4
  • 28
  • 61
  • oo this is interesting, I will keep that in mind. Didn't know that was possible. I think it's probably because they were expecting you to help OP and explain to him a bit more in-depth why it is like this – penguin Nov 28 '19 at 13:49
  • @m00ncake: Yeah, I did that no more than 5 seconds after posting the original answer (which lacked the part "Function `main` returns the user's input, so"). So I believe that the statement "very typical to the community here" explains what happened here pretty accurately! – goodvibration Nov 28 '19 at 13:51
  • "does what the user is asking" is not necessarily "== good answer". In particular if it doesn't explain *why* the problem exists and how it is solved. – mkrieger1 Nov 28 '19 at 13:53
  • @goodvibration I see it happening, can't help but feel as though the community is changing as to how it was a few years ago. Maybe because you beat em to the finish line? Haha. Also I see OP being downvoted a lot ...that's an ouchie. – penguin Nov 28 '19 at 13:55
  • @mkrieger1: Yes it does. Please read the answer. And while it may be true that the general design is bad (a function called `main` which returns a value, which is then used in global scope), the question was not about the design, and if I had gone that far to explain what's wrong with this design then I would have been way off from the original question at that point. So yes, **in the given context**, having an answer which does what the user is asking for sounds like a fairly sufficient contribution. – goodvibration Nov 28 '19 at 13:59