14,392,424 members
Rate this:
See more:
Hello, I'm in my very first programming class to become a software engineer. My question is regarding a while loop question. Below is my code:
For this challenge you must write a function named count_to_n . This function should take one argument (you can safely assume that all arguments will always be positive integers), and it should print all integer values from 1 to the argument value - one number on each line. This function must use a while loop to count from 1 to the value of the argument. So the n for this project can either be 2, 3, 5, or 10. I did a code that goes to n 10 but I was wondering how I can modify it for it to be any number and still get it right. For example if I put n as 20 the code will not work or if its 55. Sorry I'm a beginner and I'm just trying to figure out things on my own since is an online class.

What I have tried:

```def count_to_n(n):
if n == 2:
n = 1
while n <= 2:
print(n)
n = n + 1
elif n == 3:
n = 1
while n <= 3:
print(n)
n = n + 1
elif n == 5:
n = 1
while n <= 5:
print(n)
n = n + 1
elif n == 10:
n = 1
while n <= 10:
print(n)
n = n + 1
else:
return```
Posted
Updated 17-Sep-19 22:32pm
v2

Rate this:

## Solution 1

Quote:
Sorry I'm a beginner and I'm just trying to figure out things on my own since is an online class.

So no full blow up solution but advices:
- When you call the function, `n` is the last value of list to print. Don't touch it. You are allowed more than 1 variable, so use another to keep position in list.
- This site is a very good tutorial site: Python Tutorial[^]
Python While Loops[^]
The site allow you to test code. Trial and error is how you learn.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Rate this:

## Solution 2

You can have local variables in a function.
The code below simply iterates up to n, increment x each time.

```def count_to_n(n):
x=1
while x <= n:
print(x)
x += 1
return```
Member 14595948 19-Sep-19 10:07am

Thank you. Your code actually help me a lot! I can see how it works now.