I have a basic code that just simply loops over and print some numbers in python. My problem was that I needed a different way to print my output since I wanted them to be on the same line. (The standard python print statement just prints stuffs on a new line). After doing some research i discovered that I could do something like this
import random
from sys import stdout
i= 0
while i < 100:
j = 0
while j < 4:
k =0
while k < 4:
sys.stdout.write(random.randint(0, 9))
k += 1
sys.stdout.write(" ")
j += 1
i += 1
print "\n"
This works like charm when I run it using IDLE — python’s ide, my only problem is that whenever I try to run it from the terminal it complains about this
CallingCard $ python numGenerator.py
Traceback (most recent call last):
File "numGenerator.py", line 13, in <module>
sys.stdout.write(random.randint(0, 9))
NameError: name 'sys' is not defined
CallingCard $
Wondering what might be the problem since running it from IDLE, FYI I am using python 2.7.3
Found the solution. Deep thinking helps sometimes
Below is the code