(Python) Generating Numbers then Printing them on One Line
<p>I have an assignment that requires me to print 10 random sums, then get the user to enter their answers. I know how to randomly generate numbers, but I need to print the whole sum out, without giving the answer, but with the string I am using it adds them automatically.</p>

<p>This is my code below:</p>

<pre><code>for i in range(10):
        number1 = (random.randint(0,20))
        number2 = (random.randint(0,20))
        print (number1 + number2)
    answer = input("Enter your answer: ")
</code></pre>

<p>If someone could help me out I would appreciate it, I believe the line</p>

<pre><code>    print (number1 + number2)
</code></pre>

<p>is the problem, as it is adding them when I just want the random numbers printed in the sum.</p>

<p>Thanks in advance,</p>

<p>Flynn.</p>

<p>Your following line first computes the sum of the two numbers, then prints it</p>

<pre><code>print (number1 + number2)
</code></pre>

<p>you have to print a string:</p>

<pre><code>print(str(number1) + " + " + str(number2))
</code></pre>

<hr>

<p>or prefer formating features instead of concatenation:</p>

<pre><code>print("{} + {}".format(number1, number2))
</code></pre>

<p>or:</p>

<pre><code>print("%s + %s" % (number1, number2))
</code></pre>

<hr>

<p>Finally, you may want to read some doc:</p>

<ul>
<li><a href="https://docs.python.org/2.7/library/stdtypes.html?highlight=str.format#str.format" rel="nofollow">str.format</a></li>
<li><a href="https://docs.python.org/2.7/library/stdtypes.html?highlight=str.format#string-formatting" rel="nofollow">string formatting with the <code>%</code> operator</a></li>
<li><a href="https://docs.python.org/2.7/tutorial/index.html" rel="nofollow">Python tutorial</a></li>
</ul>

python random numbers sum