14 Replies - 4767 Views - Last Post: 31 July 2012 - 07:23 AM Rate Topic: -----

#1 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Turning a while loop into a function

Posted 30 July 2012 - 08:30 AM

I'm working through the "Learn Python the hard way" online book and have ran into a problem. At the end of a page he asks me to "Convert this while loop to a function that you can call, and replace 6 in the test (i < 6) with a variable." I've tried but i'm stuck and can't get it too work :(

The page of the book is: http://learnpythonth.../book/ex33.html and my code is.

def while_numbers(numbers):
	i = 0
	numbers = []
	
	while i < numbers:
		print "At the top i is %d" % i
		numbers.append(i)
		
		i += 1
		print "Numbers Now: ", numbers
		print "At the bottom i is %d" % i
		
	print "The numbers: "
	
	for num in numbers:
		print num


I know it's wrong but it's as close as i came.

Is This A Good Question/Topic? 0
  • +

Replies To: Turning a while loop into a function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Turning a while loop into a function

Posted 30 July 2012 - 08:37 AM

Both your function argument as well as the local variable on line 3 have the same name. So what actually happens is that you throw away the function argument and assign the empty list to it.
Was This Post Helpful? 0
  • +
  • -

#3 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 30 July 2012 - 08:43 AM

View Postsepp2k, on 30 July 2012 - 08:37 AM, said:

Both your function argument as well as the local variable on line 3 have the same name. So what actually happens is that you throw away the function argument and assign the empty list to it.



I've changed one of them. But i still don't think it's right. It just doesn't seem right looking at it.
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Turning a while loop into a function

Posted 30 July 2012 - 08:45 AM

To elaborate a little bit more on it:
You misunderstood the exercise. The first step only asked you to replace the while part with a function. Not the whole code. It should look like this:

def while_func(stop_point):
    pass # you have to do that

i = 0
numbers = []

while_func(6)

print "The numbers: "

for num in numbers:
    print num



Was This Post Helpful? 1
  • +
  • -

#5 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 30 July 2012 - 08:47 AM

View PostNallo, on 30 July 2012 - 08:45 AM, said:

To elaborate a little bit more on it:
You misunderstood the exercise. The first step only asked you to replace the while part with a function. Not the whole code. It should look like this:

def while_func(stop_point):
    pass # you have to do that

i = 0
numbers = []

while_func(6)

print "The numbers: "

for num in numbers:
    print num




Ha Ha thanks! No wonder it didn't look right! I guess i better go redo the functions and while loop section!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Turning a while loop into a function

Posted 30 July 2012 - 08:48 AM

Step 1, replace the 6 in while i < 6: with a variable. The variable "numbers" is NOT a valid option. You need a new variable. Test it with the value of 6, then a couple of other values. Then, that variable becomes the parameter for the function you're going to write.

Don't make this too hard. The first part in just a line for the variable declaration. The function is just putting the block of code in a def. You are really just changing one line, adding one line, and indenting a block of code.
Was This Post Helpful? 0
  • +
  • -

#7 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:03 AM

View Postbaavgai, on 30 July 2012 - 08:48 AM, said:

Step 1, replace the 6 in while i < 6: with a variable. The variable "numbers" is NOT a valid option. You need a new variable. Test it with the value of 6, then a couple of other values. Then, that variable becomes the parameter for the function you're going to write.

Don't make this too hard. The first part in just a line for the variable declaration. The function is just putting the block of code in a def. You are really just changing one line, adding one line, and indenting a block of code.


a = 6
i = 0
numbers = []

def while_fun(a):
	while i < a:
		print "At the top i is %d" % i
		numbers.append(i)

		i = i + 1
		print "Numbers now: ", numbers
		print "At the bottom i is %d" % i


	print "The numbers: "

for num in numbers:
    print num


Still don't think i'm doing it right. Once i've figured this out i'm going to go back and go over functions again.
Was This Post Helpful? 0
  • +
  • -

#8 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:07 AM

You never called that function. A function is like a programmed robot. You told it what to do, but never said "do it". You need to add that, just one more line of code.
Was This Post Helpful? 0
  • +
  • -

#9 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:18 AM

View PostNallo, on 30 July 2012 - 09:07 AM, said:

You never called that function. A function is like a programmed robot. You told it what to do, but never said "do it". You need to add that, just one more line of code.


How do i call it? Using "return while_fun" ??? Or something similar?
Was This Post Helpful? 0
  • +
  • -

#10 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:24 AM

the function doesn't need to return anything. It changes the global variables i and numbers. But you need to add a not indented line like while_fun(6), to make the code inside the function to actually run.

When python runs your programm it does so line by line. When it runs into a def something(arguments) statement it skips over the code that is indented, but remembers to execute those lines of code once it runs into a something(some_values) on an other (not indented) line.
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:24 AM

Since it's a function with side effects (modifies a global variable and prints to standard out) you can just call it and stuff will happen:

while_fun(a)


If you want one or more of the values you assemble in the function, return them, for example:

return numbers, i


will give you a tuple that you could use for debugging.

In that case, something like

return_vals = while_fun(a)



or

return_list, return_i = while_fun(a)



and now you have the list and the final value of i to play with.
Was This Post Helpful? 0
  • +
  • -

#12 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:29 AM

Yeah i'm completely confused now. I'm going to go back redo functions and while loops then come back to this problem! Thanks for all your help though.
Was This Post Helpful? 0
  • +
  • -

#13 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Turning a while loop into a function

Posted 30 July 2012 - 09:34 AM

Trying to remove a little of your confusion. Just to demonstrate what a function call is:

This Program does nothing. It is just a promise to print "Hello World" when the function is called:
def print_something():
    print("Hello World")



Now lets call the function:
def print_something():
    print("Hello World")

print_something()



or lets call it three times:
def print_something():
    print("Hello World")

print_something()
print_something()
print_something()


Was This Post Helpful? 0
  • +
  • -

#14 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: Turning a while loop into a function

Posted 31 July 2012 - 06:10 AM

I took a break and came back to it this morning! And i did it! It just clicked this morning what i was doing wrong!

a = 6

numbers = []
	

def while_fun(a):
	
	i = 0
	
	while i < a:
		print "At the top i is %d" % i
		numbers.append(i)

		i = i + 1
		print "Numbers now: ", numbers
		print "At the bottom i is %d" % i


	print "The numbers: "
	return numbers

while_fun(a)

for num in numbers:
    print num

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Turning a while loop into a function

Posted 31 July 2012 - 07:23 AM

Yes, you have it. Sort of.

To program in any language you have to understand "scope" Here, you're using global scope for "numbers."

a = 6 # outside of function
numbers = [] # outside of function
	
def while_fun(a): # note, this a is not the same as the a outside
	i = 0
	while i < a:
		print "At the top i is %d" % i
		numbers.append(i) # this numbers is the global one

		i = i + 1
		print "Numbers now: ", numbers
		print "At the bottom i is %d" % i


	print "The numbers: " # this doesn't quite make sense here
	return numbers # you return here, but the caller ignores it?

while_fun(a)

for num in numbers:
    print num

# now try this
print "call 2"
while_fun(5)

for num in numbers:
    print num

# what do you get?
# why



Now, instead, limit your scope:
def while_fun(a):
	numbers = []
	i = 0
	while i < a:
		print "At the top i is %d" % i
		numbers.append(i) # this numbers is the global one

		i = i + 1
		print "Numbers now: ", numbers
		print "At the bottom i is %d" % i
	return numbers
	
def print_results(nums):
	print "The numbers: "
	for num in nums:
		print num


print_results( while_fun(6) )
print_results( while_fun(5) )



Hope this helps.

This post has been edited by atraub: 31 July 2012 - 08:26 AM
Reason for edit:: never thought I'd have to fix your code tags! :-P

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1