[英]How do I call my printArray function in both my flipHorizontal and flipVertical functions in order to flip my code each time it is run?
I am currently taking a computer science course called Intro to Computer Science where I am learning Python (Python 3) and I doing an assignment having to do with flipping 2D arrays.我目前正在学习一门名为 Intro to Computer Science 的计算机科学课程,我正在学习 Python(Python 3),并且我正在做与翻转 2D arrays 有关的作业。 I have written some code for my assignment already and I already have it running sequentially as it tells me to in the instructions but I am having some problems flipping it each time.我已经为我的作业编写了一些代码,并且我已经按照说明中的说明按顺序运行它,但是每次翻转它时我都遇到了一些问题。 What I want to know is: How do I call my printArray function in both my flipHorizontal and flipVertical functions in order to flip my code each time it is run?我想知道的是:如何在我的 flipHorizontal 和 flipVertical 函数中调用我的 printArray function 以便在每次运行时翻转我的代码? I have the assignment instructions and an instructor's comment on my assignment.我有作业说明和教师对我的作业的评论。
Here are the instructions:以下是说明:
Here is my code:这是我的代码:
def printArray(N):
for r in N:
for c in r:
print(c,end = " ")
print()
print()
def flipHorizontal(N):
Array = []
for i in range(0,len(N)):
pos = []
for j in range(0,len(N[0])):
pos.append(N[i][len(N[0])-j-1])
Array.append(pos)
return Array
def flipVertical(N):
newArray = []
for i in range(0,len(N)):
newArray.append(N[len(N)-i-1])
return newArray
Array1 = [[0, 2, 0, 0,0], [0, 2, 0, 0,0], [0, 2, 2, 0,0], [0, 2, 0, 2,0],[0, 2, 0, 0,2]]
printArray(Array1)
flipedHor = flipHorizontal(Array1)
printArray(flipedHor)
flipedVer=flipVertical(Array1)
printArray(flipedVer)
Here is the comment:这是评论:
If I am reading correct, you just need to add print statement in your defined functions.如果我没看错,你只需要在你定义的函数中添加 print 语句。
def printArray(N):
for r in N:
for c in r:
print(c,end = " ")
print()
print()
def flipHorizontal(N):
Array = []
for i in range(0,len(N)):
pos = []
for j in range(0,len(N[0])):
pos.append(N[i][len(N[0])-j-1])
Array.append(pos)
printArray(Array)
return Array
def flipVertical(N):
newArray = []
for i in range(0,len(N)):
newArray.append(N[len(N)-i-1])
printArray(newArray)
return newArray
Array1 = [[0, 2, 0, 0,0], [0, 2, 0, 0,0], [0, 2, 2, 0,0], [0, 2, 0, 2,0],[0, 2, 0, 0,2]]
printArray(Array1)
flipedHor = flipHorizontal(Array1)
flipedVer=flipVertical(Array1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.