本文介绍了实现功能,该功能接受数组并将每个元素乘以给定的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def multAll(A, k):
    # takes an array of integers and an int, and multiplies each integer by the int.
    for i in A:
        i = i*k
        return i

# test 1

A = [5,12,31,7,25]
multAll(A, 10)
print(A)  # should print [50,120,310,70,250]

我在做多事时错了所有这一切都不能给我正确的答案吗?

What am I doing wrong in multAll that isnt giving me the correct answer?

推荐答案

在函数中第一次出现return i时,函数停止并返回当前i.

When return i happens for the first time in your function, the function stops and returns the current i.

def multAll(A, k):
    return_value = []
    for i in A:
        i = i*k
        return_value.append(i)
    return return_value

像这样,将创建一个完整列表return_value,并返回该列表.

Like this, a complete list return_value is created, and that list is returned.

这篇关于实现功能,该功能接受数组并将每个元素乘以给定的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:19