Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 6 - Functions - Programming Exercises - Page 207: 6.18

Answer

code

Work Step by Step

# 6.18 (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. import random def printMatrix(n): for i in range(1, n * n + 1): x = random.randint(0, 1) print(x, end=' ') if i % n == 0: print() def main(): n = eval(input("Enter n: ")) printMatrix(n) main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.