-1
def AskInput():
    input('''Awaiting input:
    >''')
                             # how the frick do I tell it to take the output of the above command and use it as a value for the if Statements...?

words = AskInput()
AskInput()
while words == "help":
    for i in range(1):
        print('''Type 'program1' for calculator;
    Type 'program2' for quotes.''')
    AskInput()
if words == "program1":
    program1()
if words == "program2":
    program2()

So what I'm trying to do, as the comment in my code suggests, is I'm trying to take the output of the "AskInput()" function I created and use it as the "words" value for the while loop and if statements. "words = AskInput()" doesn't feel correct but I tried it out of desperation. When I run this, the program doesn't care what I type; whatever I say for input, it will ask twice total and end. I suspect it's because of "words = AskInput()"? But then how do I phrase it to say "words = 'the result of'AskInput()"?

The first thing I tried instead of "words = AskInput()" was:

def AskInput():
    words = input('''Awaiting input:
    >''')

But for some reason "words" was greyed out and was displaying "Shadows name 'words' from outer scope" and "Local variable 'words' value is not used". I have no idea what the first message means, and I don't get why the second message exists? "words" is being used in my loop and statements, so it is used, isn't it? And of course, when I run the program it returns an error and says: "NameError: name 'words' is not defined" which it is though, in my "AskInput()" function.

Full disclosure: the program was working fine when, instead of having an "AskInput" function, I simply had:

words = input('''Awaiting input:
    >''')

And repeated the same input function at the end of the while loop, but I wanted to practice being a half-decent coder by creating a function I could easily call multiple times with limited code rather than repeating the same "input" function. Plus, I need practice with defining functions and understanding the concept.

Sorry if this feels chaotic and it's tough to understand, I'm entirely new to coding and don't yet know how to explain myself properly. I'm still trying to "internalize" the overall logic of programming so I can communicate things better. Thank you for your time! :D

Fox
  • 3
  • 1

1 Answers1

0

You just need to return the input() string from your function:

def AskInput():
    return input('''Awaiting input:
    >''')

and then capture the value as you do:

words = AskInput()

Any time you execute just:

AskInput()

then the function executes, but throws away the value.

It looks like your full program should be like this:

def AskInput():
    return input('''Awaiting input:
    >''')

words = AskInput()
while words == "help":
    print('''Type 'program1' for calculator;
    Type 'program2' for quotes.''')
    words = AskInput()
if words == "program1":
    program1()
if words == "program2":
    program2()
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Ah, "return"! I always forget it exists! Now though, whenever I run the program and lead with "help" as the input, as the following input I can type in *anything* and it is recognized as "help" (or whatever it is that's going on), and keeps spitting out "AskInput()". Plus, entering either "program1" or "program2" after "help" does not run those functions but instead does as if I typed in "help" again. – Fox Jul 19 '22 at 17:01