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

数据更新逻辑实现问题:重复执行时旧数据重置异常求助

Fixing the Resetting Old Data Issue in Your Data Update Flow

Got it, let's figure out why your code keeps resetting the data_inn value every time you run it. The core problem here is that you're redefining data_inn = 'old data' inside the function every time it executes—so no matter how many times you update it, the next run of the function will toss that updated value and start fresh with the original 'old data'.

Let's fix this by making sure the updated data state is preserved between iterations. Here are a few solid approaches:

1. Use a Global Variable (Simple, Straightforward)

Move the initial data_inn outside the function so it retains its value across function calls:

# Initialize the data outside the function to preserve state
data_inn = 'old data'

def ask_data_change():
    global data_inn  # Declare we're using the global variable
    my_data_input = input('What is your input?: ')
    
    if data_inn == my_data_input:
        print("Data hasn't changed—no action taken.")
    else:
        data_inn = my_data_input
        print(f"Updated data to: {data_inn}")
        # Repeat the flow with the new data
        ask_data_change()

# Start the process
ask_data_change()

This works because the global data_inn isn't reinitialized every time the function runs—it keeps the last updated value until you change it again.

2. Pass Data as a Function Parameter (No Global Variables)

If you want to avoid global variables (a good practice for cleaner code), pass the current data state as a parameter to the function, and recursively call it with the updated value:

def ask_data_change(current_data='old data'):
    my_data_input = input('What is your input?: ')
    
    if current_data == my_data_input:
        print("Data is the same—exiting the loop.")
        return
    else:
        new_data = my_data_input
        print(f"Updated data to: {new_data}")
        # Recurse with the updated data to repeat the flow
        ask_data_change(new_data)

# Start the process with the initial data
ask_data_change()

This way, each recursive call uses the latest updated value instead of resetting to the original.

3. Use a While Loop (Avoid Recursion Limits)

If you're worried about hitting Python's recursion limit with many updates, a while loop is a safer alternative. The data_inn is initialized once, and the loop keeps running with the current state:

def ask_data_change():
    data_inn = 'old data'
    
    while True:
        my_data_input = input('What is your input?: ')
        
        if data_inn == my_data_input:
            print("Data hasn't changed—no action taken.")
            # Uncomment the line below if you want to exit the loop when data is unchanged
            # break
        else:
            data_inn = my_data_input
            print(f"Updated data to: {data_inn}")

# Start the loop
ask_data_change()

This approach keeps the data state within the loop, so it never resets unless you restart the function.

Key Takeaway

The original issue happened because your initial data_inn was trapped inside the function's scope, getting recreated every time the function ran. By preserving the data state outside the function's reinitialization (either via global variables, parameter passing, or a loop), you ensure the updated value sticks around for the next iteration.

内容的提问来源于stack exchange,提问作者laks

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:05:43