Jan-15-2021, 12:46 PM
If I follow the example
But if I follow the text description:
>>> def add_num_to_list(lst, num): ... new_list = [lst[0]] ... for i in lst[1:]: ... new_list.append(i+num+new_list[-1]) ... return new_list ... >>> print(add_num_to_list([0,0,0,0], 25)) [0, 25, 50, 75] >>> print(add_num_to_list([5,10], 25)) [5, 40]Where the last printout is an answer to the question asked by @bowlofred
But if I follow the text description:
>>> def add_num_to_list(lst, num): ... new_list = [] ... for i in lst: ... new_list.append(i+num+(new_list[-1] if new_list else 0)) ... return new_list ... >>> print(add_num_to_list([0,0,0,0], 25)) [25, 50, 75, 100] >>> print(add_num_to_list([5,10], 25)) [30, 65] >>>