【发布时间】:2020-04-23 19:05:17
【问题描述】:
问题
编写一个名为 stop_at_four 的函数,它遍历一个数字列表。使用 while 循环,将每个数字附加到新列表中,直到数字 4 出现。该函数应返回新列表。
我的回答
def stop_at_four():
list_=[3,6,4,1,3]
accum_lst=[]
accum_var=0
while list_[accum_var] !=4:
accum_lst.append(list_[accum_var])
accum_var+=1
return
附:任何关于如何提高我的代码效率的批评将不胜感激。
【问题讨论】:
-
accum_var += 1必须在while内。
标签: python list while-loop