You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python初学者求助:如何获取自定义AskInput函数的返回值以用于while循环和if语句

Fixing Your 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 words variable 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 words but didn't do anything with it (like return it). Since you still weren't returning a value, words = AskInput() still set words to None—leading to the NameError when 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 return to AskInput(): This sends the user's input back to wherever the function is called, so words = AskInput() actually gets the input value instead of None.
  • Updated words inside the while loop: Without this, words would stay stuck on its initial value, creating an infinite loop if the user first entered "help".
  • Used elif instead of a second if: It's cleaner and more efficient since only one of these conditions can be true at a time.
  • Added placeholder program1() and program2() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 22:02:51