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

Python函数无法向列表追加值并返回结果,请求技术协助

Fixing Your Python Product Function Issue

Hey there! I see the problem with your code right away—your function is doing all the work to build the list of products, but it's not actually sending that list back to you when you call it. Let me break it down:

The Root Cause

In Python, if a function doesn't explicitly use a return statement to send a value back, it defaults to returning None. That's why when you run print(Product(300)), you're seeing None instead of your expected list of products. Your Products list is being created and populated correctly inside the function, but it stays trapped there unless you return it.

The Fixed Code

Here's how to adjust your function to make it work as intended:

def Product(number):
    Products = []
    for i in range(100, 999):
        Products.append(number * i)
    return Products  # This line was missing!

print(Product(300))

What Changed?

I added a return Products statement at the end of the function. This tells Python to send the completed list back to wherever the function was called. Now when you run print(Product(300)), you'll see the full list of 300 multiplied by every integer from 100 to 998 (since range(100, 999) stops at 998).

Bonus Tip

If you want to make your code a bit more concise, you could even use a list comprehension instead of a for loop—this is a common Pythonic approach for building lists like this:

def Product(number):
    return [number * i for i in range(100, 999)]

print(Product(300))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:33:00