Python函数无法向列表追加值并返回结果,请求技术协助
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

