0

I'm missing something very simple. I'm a new Python student, so bear with me.

for i in range(10):    
    print("\n")
    for x in range(10):
        print(x, end = " ")

Sample output:

0 1 2 3 4 5 6 7 8 9    
0 1 2 3 4 5 6 7 8 9    
0 1 2 3 4 5 6 7 8 9    
...

The "i" variable is setting the size of what I want to output, and in the above example I am showing the range of 0-9, 10 times. How would I alter this to display the following without using the print command by way of a string or an array? I'm trying to use two nested "for" statements, but I'm drawing a blank on what to use in place of the range command.

Desired output:

0 0 0 0 0 0 0 0 0 0    
1 1 1 1 1 1 1 1 1 1    
2 2 2 2 2 2 2 2 2 2    
3 3 3 3 3 3 3 3 3 3    
...

3 Answers 3

1

You are printing the variable from your inner loop (x) instead of the variable from the outer loop (i). Do it this way:

for i in range(10):

    print("\n")
    for x in range(10):
        print(i, end = " ")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for the quick response! That was exactly what I was overlooking!
0

why to loop twice, we know str*n give n times str

>>> for x in range(10):
...     print((str(x) + ' ')*10)
... 
0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 
2 2 2 2 2 2 2 2 2 2 
3 3 3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 9 

demo:

>>> 'a'*5
'aaaaa'
>>> 'a'*10
'aaaaaaaaaa'
>>> 'hello**'*10
'hello**hello**hello**hello**hello**hello**hello**hello**hello**hello**'
>>> '9'*10
'9999999999'

Comments

0

This also works:

for i in range(10):
  print ' '.join(str(i) * 10)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.