-3

I am having an issue printing random numbers to the same line, then printing the total of the 6 random numbers on the second print statement. Here is what i have so far.

import random
def main():

    randnums()

def randnums():
    for num in range(0, 6):
        num1 = random.randint(1,9) 
        print(str(num1))
        print(" The total is: ")

main()

Should look like this

  2 4 8 9 9 3 
  The total is 35
G. Yedkois
  • 33
  • 2
  • 11

5 Answers5

1

I'm not sure what is wrong with your code because of its poor indentation but here are some things to take note of:

  1. You need to have a variable which stores the total
  2. Python print function defaults all strings to print out with a newline.

Store total

This is simple, just add a variable outside of the for loop which adds the number:

def randnums():
    # Add this new variable
    total = 0
    for num in range(0, 6):
        num1 = random.randint(1, 9)
        total += num1
    # Print it out
    print("The total is:", total)

Remove newline

For Python 3, it is simple to remove the newline. Just supply a new keyword argument to the print function and it will work:

def randnums():
    total = 0
    for num in range(0, 6):
        num1 = random.randint(1, 9)
        total += num1
        # End the string with a space. Also removes newline
        print(num1, end=" ")
    # Add a newline before printing again.
    print("\nThe total is:", total)

If you are using Python 2 in the future, you could do it two ways: Either import print_function from __future__ or print with a comma:

Import from __future__:

# Note that this import must come first
from __future__ import print_function
import random

def main():
    randnums()

def randnums():
    total = 0
    for num in range(0, 6):
        num1 = random.randint(1, 9)
        total += num1
        print(num1, end=" ")
    print("\nThe total is:", total)

main()

Or use a comma:

import random

def main():
    randnums()

def randnums():
    total = 0
    for num in range(0, 6):
        num1 = random.randint(1, 9)
        total += num1
        # The comma is put after what you want to print. It also adds a space
        print num1,
    print "\nThe total is:", total

main()

The output:

3 9 8 2 4 1
The total is: 27
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
  • +1 These are all excellent solutions, the only other one I would suggest would be to store them in a list, then print the whole list, which I would expect to be faster than six separate calls to print individual characters, but it's less pythonic and in this basic case wholly unnecessary. – TemporalWolf Jun 24 '16 at 04:53
  • @TemporalWolf Just tested some code. Using a list is actually slower. I am guessing it's because lists are more expensive compared to ints due to its expandability. – Moon Cheesez Jun 24 '16 at 05:00
  • PyCharm agrees with you, in the naive case. With list comprehension, it's much, much faster: 100000 elements (in sec) Time for print 0.906000 Time for list 0.994000 (0.011000 to build list, 0.983000 to print) Time for list comp 0.024000 – TemporalWolf Jun 24 '16 at 06:02
  • Try `print ''.join([repr(random.randint(1,9)) + " " for num in xrange(count)])` :) – TemporalWolf Jun 24 '16 at 06:09
0

You will have better luck if you indent your code correctly.

def randnums():
    for num in range(0, 6):
        num1 = random.randint(1,9) 
        print(str(num1), end=" ")
    print()
    print(" The total is: ")

And to you need to actually accumulate the total in a variable which is trivial because summing is very basic and everything basic is very well documented.

0

The correct code is as follows:

import sys
def randnums():
    sum = 0
    for num in range(0, 6):
        num1 = random.randint(1,9) 
        sum += num1
        sys.stdout.write(str(num1)+" ")
    print("\nThe total is: "+str(sum))

OR (if Python 3)

def randnums():
    sum=0
    for num in range(0, 6):
        num1 = random.randint(1,9) 
        sum += num1
        print(str(num1), end=' ')
    print("\nThe total is: "+str(sum))
Nagabhushan Baddi
  • 1,164
  • 10
  • 18
0

See this: Print new output on same line

In addition:

  1. Make sure to fix your indentation; python is very strict about it.

  2. You need to either save the numbers in a list or accumulate the sum in a variable. Otherwise after you print num1, it's gone.

    import random
    
    total = 0
    for num in range(0, 6):
      num1 = random.randint(1,9)
      total += num1
      print(num1, end=' ')
    print('\nThe total is:', total)
    
Community
  • 1
  • 1
neuranna
  • 101
  • 2
0

In python 2.7

 import random
    def main():

        randnums()

    def randnums():
        sum=0
        for num in range(0, 6):
            num1 = random.randint(1,9) 
            print(str(num1))
            sum+=num1,
        print
        print(" The total is: %d" % sum)

    main()

In python 3

def randnums():
        sum=0
        for num in range(0, 6):
            num1 = random.randint(1,9) 
            print("{}".format(num1)),
            sum+=num1
        print
        print("{}{}".format(" The total is: " , sum))