Python帮助-创建基本函数(2.7版)

Python帮助-创建基本函数(2.7版),python,Python,我在这个问题上取得了进展,但它总是只返回列表中的第一个值。我的代码中缺少了什么 编写一个名为add_numbers的函数,该函数接收一个列表参数。它返回列表中从开始到至少找到10的所有数字的总和。如果未找到大于或等于10的数字,则返回列表中所有数字的总和 第二个是: 编写一个名为make_list的函数,该函数接收一个数值参数。它返回一个从0到小于数值参数1的数字列表 如果问我所有数字的和,我知道怎么做,但是我被这个列表弄糊涂了 最后一点是: 编写一个名为count_bricks的函数,该函数接

我在这个问题上取得了进展,但它总是只返回列表中的第一个值。我的代码中缺少了什么

编写一个名为add_numbers的函数,该函数接收一个列表参数。它返回列表中从开始到至少找到10的所有数字的总和。如果未找到大于或等于10的数字,则返回列表中所有数字的总和

第二个是:

编写一个名为make_list的函数,该函数接收一个数值参数。它返回一个从0到小于数值参数1的数字列表

如果问我所有数字的和,我知道怎么做,但是我被这个列表弄糊涂了

最后一点是:

编写一个名为count_bricks的函数,该函数接收一个数字参数。此函数返回金字塔中的砖块数,该砖块数高出许多层。金字塔中的每一层都比它上面的层多一块砖

不知道从哪里开始


提前谢谢你的帮助。这不是家庭作业,只是一个充满问题的小测验——这些是我无法回答的问题。

您必须将return置于循环之外,否则值将在第一次迭代时返回

def add_numbers(a):
    total = 0
    i = 0
    while a[i] < 10 and i < len(a):
        total = total + a[i]
        i = i + 1
    return total        # return  should be outside the loop
第二个问题的提示:

创建一个接受一个输入的函数 使用内置函数返回新列表。 第一个问题:

添加检查以在列表结束时结束循环:

while a[i] < 10 and i < len(a):
第二个问题:


了解Python的特性。只需循环数字的时间并将数字添加到列表中。最后返回该列表。

请格式化问题中的代码。我怀疑您的第一部分中的问题与return语句的位置有关,但是如果没有格式化的代码,很难判断。还有,这是家庭作业吗?请输入你的代码。一次一个问题。如果你不能得到正确的缩进,你可能想考虑学习一种不同的语言。这不仅仅是因为它看起来很漂亮。它必须是正确的,因为它改变了节目的意义
def add_numbers(a):
    """
        returns the total of all numbers in the list from the start, 
        until a value of least 10 is found. If a number greater than 
        or equal to 10 is not found, it returns the sum of all numbers in the list.
    """
    total = 0
    index = 0
    while index < len(a):
        total = total + a[index]
        if a[index] >= 10:
            break
        index += 1
    return total
def add_numbers(a):
    """
        returns the total of all numbers in the list from the start, 
        until a value of least 10 is found. If a number greater than 
        or equal to 10 is not found, it returns the sum of all numbers in the list.
    """
    total = 0
    index = 0
    while index < len(a):
        total = total + a[index]
        if a[index] >= 10:
            break
        index += 1
    return total