0

I need to design a function named firstN that, given a positive integer n, displays on the screen the first n integers on the same line, separated by white space.

An example of using the function could be:

>>> firstN(10)
0 1 2 3 4 5 6 7 8 9

I have done this:

def firstN(n):
    for i in range(10):
        print (i, end=" ")
firstN(10);

but I can't put end=" " because my teacher has a compiler that doesn't allow it

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

2 Answers2

1

Thanks for your answers. Yes, my teacher was using the version 2 of python. I have alredy solved the question. To print without a newlineyou need to put a comma:

def firstN(n):
    for i in range(10):
        print (i,)
firstN(10);
  • In Python 2 you also need to remove the parentheses. `(1,)` creates a tuple, which is not at all what you want. – Dan Getz Nov 01 '22 at 15:06
  • Please tell your teacher to update this. Even the most recent 2.x versions are as outdated as Windows 7 is, and they equally have **not been supported** for three full years now. – Karl Knechtel Jan 02 '23 at 00:19
0

You can use starred expression to unpack iterable arguments:

def firstN(n):
    print(*(range(n)))

firstN(10)

# 0 1 2 3 4 5 6 7 8 9
Arifa Chan
  • 947
  • 2
  • 6
  • 23