i have got following function:
def second(n): z =0 while z < n: z = z +1 in range(n): print z the output second(3):
1 1 1 2 2 2 3 3 3 i've tried several things, seem unable store "results" in list further use. has idea should try?
print doesn't save results - sends them (one @ time) standard output (stdout) display on screen. in order copy of results humbly suggest turn generator function using yield keyword. example:
>>> def second(n): ... z =0 ... while z < n: ... z = z +1 ... in range(n): ... yield z ... >>> print list(second(3)) [1, 1, 1, 2, 2, 2, 3, 3, 3] in above code list() expands generator list of results can assign variable save. if want save list of results second(3) alternatively results = list(second(3)) instead of printing it.
Comments
Post a Comment