lundi 5 janvier 2015

How to randomly get 0 or 1 every time for k?

Display matrix of 0s and 1s



Write a function that displays an n-by-n matrix using the following header:



def printMatrix(n):


Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run:



Enter n: 3
010
000
111


Here is my idea:



from __future__ import print_function

from random import randint, choice

import random

n = input ("Enter an interger number:")

k = randint(0, 1)

for i in range (1, n+1):

for j in range (1, n+1):

print (format((i*j)/(i*j)*k, "3d") , end = '')

print ()




Aucun commentaire:

Enregistrer un commentaire