I'm new to python and trying to run a function that will, given one variable, count down to zero and then up to the original variable. the output should look something like this:

>>> functionname(5) 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 

so far I've written the code below, but this doesn't count up all the way to the original variable. I guess I need to somehow save the variable in order to refer to it later, but I have no idea how to do that, since python automatically changes n as the function goes on.

def functionname(n): n = orginal while n > 0: print n n=n-1 print n if n==0: print n n=n+1 

I would be very grateful for some pointers, as I seem to be completely stuck at the moment.

6 Answers

Just count from negative to positive and use math:

def fn(n): print ', '.join(str(abs(i)) for i in range(-n, n+1)) fn(5) # 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 

Pointers:

  • If you already know the range you want to iterate over, it's much cleaner and more direct to use a for loop instead of a while.
  • You can use the range function to generate number sequences.
  • You can convert simple for loops into list-comprehensions as I did above.

The "simple" clean implementation of your requirements would look something like this:

def fn(n): for i in range(n, 0, -1): print i, for i in range(n + 1): print i, 

Other notes:

  • range can count backwards too
  • The end argument to range isn't included in the range itself, that's why the second loop specifies n + 1 as the limit (instead of just n)
  • Adding a trailing comma on your print makes it add a space at the end instead of a line-break.

Your second block is an if n == 0: (which you know it is since the while loop terminated when n hit 0); presumably you want while n <= 5.

Note that there are nicer ways to accomplish the same thing in Python. For example, using a pair of ranges with itertools.chain to iterate each range one after another allows you to simplify to:

import itertools def functionname(n): for i in itertools.chain(range(n, 0, -1), range(n+1)): print i 

Personally, I'd do something like...

def count(n): for x in range(n, -n, -1): print(str(abs(x)) + ",") 

At the suggestion of dlewin, here's a list comprehension of the same...

def count(n): print(','.join(str(abs(x)) for x in range(n, -n, -1))) 
2

You need a second while loop that starts at 0 and goes back up to "original". Do you know about "for" loops yet? Those are better for counting.

Your idea about having original is correct however you are using the assignment operator the wrong way 'round. Also the if n==0 line should be another loop (while or for as suggested by other answers), counting back up to original.

So I'd start with copying the value from n to original like this:

original = n 

Hope that helps!

You got some bad formatting there. Remember to indent properly for functions and while and if statements.

So first, set n to 5. Then count down from there until you reach 0 with a while loop:

while n != -1: print n n -= 1 

Then after the loop breaks, count back up again and reset n to 0:

n = 0 while n < 6: print n n += 1 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.