Python lists, modules, and loops (syntax error) -
currently learning python i'm little stumped particular question.
i need print length of list using loop without use of built-in functions.
i understand following works when input it:
listlength = 0 list1 = ['a', 'b', 'c', 'd'] y in list1: listlength += 1 print(listlength)
however question gives file cannot edit:
import function_lists list1 = ['a', 'b', 'c', 'd'] print("length of list:", function_lists.listlength(list1))
in editable file named function_lists i've done this:
def length(a_list) length = 0 y in list1: listlength += 1
syntax error: list1 not defined - can tell i'm misunderstanding how modules work, shouldn't list1 pulled out of un-editable file via import function_lists
work?
i'd ask prac tutor don't have until late week.
you should using parameter name, a_list
, instead of parameter passed, list1
. @andy g pointed out, when declaring functions, need end declaration colon(eg. def function_name(args):
). moreover, default return value of function none so, @riad pointed out, should return length
variable.
def length(a_list) length = 0 y in list1: listlength += 1
should be
def length(a_list): length = 0 y in a_list: length += 1 return length
Comments
Post a Comment