How do I hide a word in a random number matrix?
<p>I am a Python beginner and am stuck here. I have written the following program that generates a matrix of random numbers.</p>

<pre><code>def randsq(size):
    for i in range(size):
        for j in range(size):
            print(random.randint(0,9), end = '')
        print()
</code></pre>

<p>Is there anyway I can add a function that takes a word from an input parameter and hides it diagonally within the matrix? So if the user inputs 'rain', the output looks like this:</p>

<pre><code>r123
1a45
23i4
989n
</code></pre>

<p>Thanks!</p>

<p>Modified code:</p>

<pre class="lang-python prettyprint-override"><code>import random
def randsq(word):
    size = len(word)
    for i in range(size):
        for j in range(size):
            if i == j:  # we are at a diagonal-value
                print(word[i], end='')  # it's the i-th diag -&gt; choose i-th char
            else:
                print(random.randint(0, 9), end='')
        print()

randsq('Rain')
</code></pre>

<p>Output:
</p>

<pre><code>R757
0a91
02i9
757n
</code></pre>

<p>If you want the user to input this word:</p>

<pre><code>word_to_hide = input('Which word to hide? (+enter) ')  # will wait for user-input + enter
randsq(word_to_hide)
</code></pre>

python algorithm matrix crossword