I am in Introduction to Programming using Python so I need help creating a Pytho
ID: 3773857 • Letter: I
Question
I am in Introduction to Programming using Python so I need help creating a Python program. Here is the exercise I need a simple, easy to understand program for:
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. Here is a sample run:
Enter n: 3
0 1 0
0 0 0
1 1 1
Any help with providing a program for this exercise would be appreciated.
Explanation / Answer
#Display matrix of 0s and 1s #write a fucntion 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: #Entern:3 #010 #000 #111 import random def printMatrix(n): i=0 for i in range (n): for i in range (n): print(random.randint(0, 1),end='') print() def main(): matrixSize=eval(input("please enter the number of elements in the matrix: ")) printMatrix(matrixSize) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.