Python含if语句函数执行打印异常问题及低算力优化需求
Let's break down your issues step by step, starting with getting your code to run correctly, then optimizing it for low-power video pipeline use.
First: Fixing the Execution Bug
Right now, your code isn't outputting the expected correction because of three key issues:
You're printing the function object, not executing it
The lineprint (correction)outputs the function's memory address because you're referencing the function itself, not running it with your pre-assignedDiffvalue. You need to call it withcorrection(Diff)instead.Python 3 Print Syntax Mismatch
Yourprint "correction X"lines use Python 2 syntax. In Python 3 (the standard in Visual Studio), print requires parentheses:print("correction X").Incorrect Return Statement
You're returning the function itself (return correction) instead of the correction result. It's better to store the correction string in a variable and return that—this lets you both print it and reuse the value if needed later.
Here's the fixed basic version of your code:
# Assume Diff is already assigned a value here, e.g.: # Diff = 25 def correction(Diff): result = "carry on" if 1 <= Diff < 20 : result = "correction 1" # is correction necessary? elif 20 <= Diff < 49: result = "correction 2" # Slight turn to right elif 50 <= Diff < 74: result = "correction 3" # turn to right elif 75 <= Diff < 100: result = "correction 4" # 'sharp' turn to right elif 100 <= Diff: result = "correction 9" # STOP elif -20 <= Diff < 0: result = "correction 5" # is correction necessary? elif -50 <= Diff < -20: result = "correction 6" # Slight turn to left elif -75 <= Diff < -50: result = "correction 7" # turn to left elif -100 < Diff < -75: result = "correction 8" # 'sharp' turn to left elif Diff <= -100: result = "correction 9" # STOP print(result) return result # Call the function with your Diff value and print the result print(correction(Diff))
Second: Low-CPU Optimization for Video Scenarios
Since this will run in a video pipeline (likely per frame), we want to minimize branching overhead and redundant operations. Here are two optimized approaches, prioritized for readability and efficiency:
Approach 1: Ordered Interval List (Best Balance of Speed & Maintainability)
Instead of a long chain of if-elif, define a list of tuples mapping (lower_bound, upper_bound, correction_value) ordered from most extreme to least. We loop through this list once to find the first matching condition—this reduces conditional branching and makes it easy to adjust ranges later.
To save extra CPU per frame, define the interval list once outside the function (so it's only initialized once, not every time you call the function):
# Define intervals once at script start (not per frame) CORRECTION_INTERVALS = [ (100, float('inf'), "correction 9"), # STOP (75, 100, "correction 4"), # sharp right (50, 75, "correction 3"), # right (20, 50, "correction 2"), # slight right (1, 20, "correction 1"), # right check (-20, 0, "correction 5"), # left check (-50, -20, "correction 6"), # slight left (-75, -50, "correction 7"), # left (-100, -75, "correction 8"), # sharp left (-float('inf'), -100, "correction 9") # STOP ] def correction(Diff): for lower, upper, res in CORRECTION_INTERVALS: if lower <= Diff < upper: print(res) return res print("carry on") return "carry on"
Approach 2: Direct Mathematical Mapping (Max Efficiency)
If your ranges won't change often, you can use math to calculate the correction directly, eliminating loops entirely. This is the fastest option for low-CPU usage:
def correction(Diff): if abs(Diff) >= 100: res = "correction 9" elif Diff > 0: if Diff < 20: res = "correction 1" elif Diff < 50: res = "correction 2" elif Diff < 75: res = "correction 3" else: # 75 <= Diff <100 res = "correction 4" elif Diff < 0: abs_diff = -Diff if abs_diff <20: res = "correction 5" elif abs_diff <50: res = "correction 6" elif abs_diff <75: res = "correction 7" else: #75 <= abs_diff <100 res = "correction 8" else: # Diff ==0 res = "carry on" print(res) return res
内容的提问来源于stack exchange,提问作者David Guest

