Python初学者求助:如何获取自定义AskInput函数的返回值以用于while循环和if语句
AskInput() Function & Variable Scope Issues Let's break down what's going wrong here and fix it step by step:
1. Why your first code wasn't working
Your original AskInput() function calls input() but doesn't return the value it captures. By default, any Python function without a return statement sends back None. So when you ran words = AskInput(), words was set to None—which is why none of your while or if logic triggered, and the program just asked for input twice before exiting.
2. Why your modified function had errors
When you updated the function to:
def AskInput(): words = input('''Awaiting input: >''')
You hit two issues:
- "Shadows name 'words' from outer scope": The local
wordsvariable inside your function shares the same name as the variable you're trying to use outside the function. Python warns you that you're "hiding" the outer variable with a local one. - "Local variable 'words' value is not used": You created the local
wordsbut didn't do anything with it (like return it). Since you still weren't returning a value,words = AskInput()still setwordstoNone—leading to theNameErrorwhen you tried to use it in your conditional checks.
3. The Correct Solution
The fix is straightforward: make your AskInput() function return the input value, and update words every time you call the function (especially inside your loop, so the condition can check the latest user input).
Here's the revised, working code:
def AskInput(): # Capture input and return it immediately to the caller return input('''Awaiting input: >''') # Placeholder functions for your programs (add your logic here) def program1(): print("Calculator program is running!") def program2(): print("Quote generator program is running!") # Get initial input and assign to words words = AskInput() while words == "help": print('''Type 'program1' for calculator; Type 'program2' for quotes.''') # Update words with new input to exit the loop if needed words = AskInput() # Use elif for cleaner, more efficient conditional checks if words == "program1": program1() elif words == "program2": program2()
Key Fixes Explained:
- Added
returntoAskInput(): This sends the user's input back to wherever the function is called, sowords = AskInput()actually gets the input value instead ofNone. - Updated
wordsinside thewhileloop: Without this,wordswould stay stuck on its initial value, creating an infinite loop if the user first entered "help". - Used
elifinstead of a secondif: It's cleaner and more efficient since only one of these conditions can be true at a time. - Added placeholder
program1()andprogram2()functions: Your original code references these but doesn't define them, so I added simple versions to avoid runtime errors.
Quick Note on Variable Scope
Variables defined inside a function (like your local words in the modified function) are only accessible inside that function unless you return them or use global variables (which is almost never the right approach for cases like this). Using return is the clean, standard way to pass values from a function to the rest of your code.
内容的提问来源于stack exchange,提问作者Fox

