n = 10
while n > 0:
print(n)
n=n-1
so far i have this which gives me
10
9
8
7
6
5
4
3
2
1
but I want
10 9 8 7 6 5 4 3 2 1
n = 10
while n > 0:
print(n)
n=n-1
so far i have this which gives me
10
9
8
7
6
5
4
3
2
1
but I want
10 9 8 7 6 5 4 3 2 1
You should use the 'end' parameter of print. By default, end="\n" in print, a newline character
n = 10
while n > 0:
print(n, end=" ")
n=n-1
There are several ways you could do that.
Firstly, and probably the simplest, you can use the end
argument of print().
For instance, you could do something like this:
n = 10
while n > 0:
print(n, end=" ") # note the 'end' argument
n=n-1
Which gives : 10 9 8 7 6 5 4 3 2 1
(with a trailing space and without carriage return).
Another way I can think of, but involves a bit more complexity in my opinion, is using join().
Example:
print(" ".join(str(i) for i in range(10, 0, -1)))
Which gives : 10 9 8 7 6 5 4 3 2 1
(with a carriage return and without trailing space).