简体   繁体   English

如何在我的 FlipHorizontal 和 FlipVertical 函数中调用我的 printArray function 以便在每次运行时翻转我的代码?

[英]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.

相关问题 每次运行代码时,如何为Turtle设置随机速度? - How do I set a random speed for my Turtle each time I run the code? 如何从我的函数中调用导入库的函数? - How do I call the functions of the imported library from my function? 如何使我的代码在我的一个 function 中包含两个“while”循环? - How do I make it so my code includes both "while" loops in my one function? 如何在我的主代码旁边运行一个函数? - How do I run a function alongside my main code? 如何在我的列表视图中的每个帖子上运行一个函数并将一个布尔变量传递到我的 html 中? - How do I run a function on each post in my list view and pass a boolean variable into my html? 如何在 python 中为 int 和 string 类型调用 function。 我附上了我的代码片段 - How to call a function for both int and string type in python. I have attached the snippets of my code 我需要通过我的代码进行解释,我们需要在哪里定义 init 方法并声明“self”。 如何在每种情况下调用 function 在我的代码中给出 - I need an explanation through my code, where do we need to define init method and declare "self". How to call a function in each cases give in my code 如何在我的Swift软件应用程序中为我的函数计算运行Python代码? - How do I run Python code for my function calculations in my Swift software App? 我应该怎么做才能减少代码的运行时间? - What should I do to reduce run time of my code? 如何修改我的代码以适合 function 以使其正常运行? - How can I modify my code to fit the function in order for it to run correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM