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

函数间变量传递问题:Python交通流量计算代码调试求助

Fixing Traffic Flow Calculation: Variable Passing & Logic Issues

Let's walk through fixing your code step by step—you're running into two main problems: incorrect variable passing between functions, and broken conditional logic in the number_of_busy_days function.

First, Fix the traffic_intensity Function

Your original code has a small oversight: converting count to an integer doesn't do anything unless you assign the result back to the variable. Here's the corrected version:

def traffic_intensity(count):
    """Number of cars passing by"""
    # Convert count to integer and assign it back to the variable
    count = int(count)
    if count < 5000:
        level = "Very Low"
    elif 5000 <= count < 10000:
        level = "Low"
    elif 10000 <= count < 18000:
        level = "Moderate"
    elif count >= 18000:
        level = "High"
    return level

Next, Fix the number_of_busy_days Function

This is where the variable passing and logic issues are most noticeable:

  1. You tried to pass level to traffic_intensity, but level is a variable only defined inside traffic_intensity—it doesn't exist in the scope of number_of_busy_days. Instead, you need to pass the current count from your loop.
  2. Your conditional check if traffic_intensity(level) == "Moderate" or "High" is invalid Python syntax for what you want. The or "High" part will always evaluate to True (non-empty strings are truthy), so every day would be counted as busy.
  3. You didn't finish incrementing the daily_counts variable.

Here's the corrected function:

def number_of_busy_days(counts):
    """Busy days based on traffic flow"""
    daily_counts = 0
    for count in counts:
        # Get the traffic level for the current day's count
        current_level = traffic_intensity(count)
        # Check if the level is Moderate OR High
        if current_level == "Moderate" or current_level == "High":
            daily_counts += 1
    # Return the total number of busy days
    return daily_counts

How to Test It

You can test this with a sample list of daily counts to make sure it works:

# Sample daily traffic counts
daily_traffic = [4500, 8000, 12000, 20000, 17000]
print(number_of_busy_days(daily_traffic))  # Should output 3 (days with 12000, 20000, 17000)

Key Takeaways

  • Variable Scope: Variables defined inside a function are only accessible within that function. To use a function's output, you need to call the function with the correct input parameters and store its return value.
  • Conditional Logic: When checking multiple values with or, you need to repeat the full comparison each time (or use in to check membership in a list, like if current_level in ["Moderate", "High"]).
  • Type Conversion: Always assign the result of type conversion back to the variable—otherwise, the conversion has no effect.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:52:36