发布附加文件python [英] issue appending files python

查看:52
本文介绍了发布附加文件python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个开源项目,目的是生产可以在终端上运行的俄罗斯方块游戏。我目前无法参加第三项考试。我写的代码通过了第一次测试。和第二次测试。并通过了针对测试3的眼图测试。但是测试3需要从测试2中获取输出并在测试3的开始处插入。我认为我可以通过在其后面加上 >>来实现。这就是问题所在。
第一个测试是将一个名为learningtris.py的文件保存到testris文件夹,然后使用以下命令运行它:

I am currently working on an open source project with the goal of producing a tetris game that can run in the terminal. I'm currently stuck on the third test. I have written code that passes the first test. and the second test. and passes the eye test for test 3. But test 3 requires taking the output from test 2 and inserting at the start of test 3. I figured I would do this by appending it '>>'. that's where the problem comes in. The first test is to save a file called learntris.py to the testris folder and then run it with:

testris.py --shell ./learntris.py

我通过了它每次。然后,它运行learntris.py中的代码以产生tetris板输出。它运行 p命令以打印出木板。该游戏中的俄罗斯方块板是由‘。’制成的10x22矩阵。
这是learntris.py中通过测试2的代码:

I pass it every time. Then it runs the code in learntris.py to produce the tetris board output. It runs the 'p' command to print out the board. the tetris board in this game is 10x22 matrix made of '.'. This is the code in learntris.py to pass test 2:

def test2():
    while True:
        qp = raw_input('')
        if qp == 'p':

            for i in range(0, 22):
                for j in range(0, 10):
                    print'.',
                print''

        else:
            return exit()


test2()

有关test 2输出的更多信息,您可以在这里:< a href = https://github.com/LearnProgramming/learntris/blob/master/testplan.org# Establishment-a-way-to-print-the-matrix-p rel = nofollow noreferrer> https:/ /github.com/LearnProgramming/learntris/blob/master/testplan.org# Establishment-a-way-to-print-the-matrix-p

For more info on the test 2 output you can go here: https://github.com/LearnProgramming/learntris/blob/master/testplan.org#establish-a-way-to-print-the-matrix-p

现在进入测试3,这是打印俄罗斯方块形状的g命令。它们由各种字母表示。这是您在测试3失败后收到的文本输出:

Now comes test 3, which is the g command that prints the tetris shapes. They are represented by various letters. This is the text output you receive after failing test 3:

 The 'g' command instructs learntris to read 22 lines
of text from the standard input stream, and use the
characters on these lines to populate some internal
representation of the matrix.

The letter 'g' is a mnemonic for the word 'given', as
in: "given the following matrix...."

The input format should be identical to the output
produced by the 'p' command.

The letters used in the representation correspond to
the set of colors used for blocks in the game:

. = empty (black)     b = blue         c = cyan
g = green             m = magenta      o = orange
r = red               y = yellow

这是我用于测试3的代码。我知道应该有某种标准输入/输出来接收'p'命令的输出,但是我不知道该怎么办。该代码主要集中在再现正确的输出上,您可以在这里看到:
> https://github.com/LearnProgramming/learntris/blob/master/testplan.org# Establishment-a-way-to-set-整个矩阵-g

This is the code I am using for test 3. I know there is supposed to be some sort of standard input/output to receive the output from the 'p' command but I'm not sure what to do there. The code is mostly focused on reproducing the correct output which you can see here: https://github.com/LearnProgramming/learntris/blob/master/testplan.org#establish-a-way-to-set-the-entire-matrix-g

def test3():
    while True:
        qp = raw_input('')
        if qp == 'g':

            for i in range(0, 4):
                for j in range(0, 10):
                    print'.',
                print''
            for i in range(4, 5):
                for j in range(0, 10):
                    print'm',
                print''
            for i in range(5, 6):
                for j in range(0, 10):
                    print'b',
                print''
            for i in range(6, 7):
                for j in range(0, 10):
                    print'c',
                print''
            for i in range(7, 8):
                for j in range(0, 10):
                    print'y',
                print''
            for i in range(8, 9):
                for j in range(0, 10):
                    print'o',
                print''
            for i in range(9, 10):
                for j in range(0, 10):
                    print'r',
                print''
            for i in range(11, 15):
                for j in range(0, 10):
                    print'.',
                print''
            for i in range(15, 17):
                for j in range(0, 1):
                    print'c',
                for j in range(1, 10):
                    print'.',
                print''
            for i in range(17, 18):
                for j in range(0, 1):
                    print'c',
                for j in range(1, 5):
                    print'.',
                for j in range(5, 6):
                    print'g',
                for j in range(6, 10):
                    print'.',
                print''
            for i in range(18, 19):
                for j in range(0, 1):
                    print'c',
                for j in range(1, 3):
                    print'.',
                for j in range(3, 4):
                    print'o',
                for j in range(4, 5):
                    print'.',
                for j in range(5, 7):
                    print'g',
                for j in range(7, 10):
                    print'.',
                print''
            for i in range(19, 20):
                for j in range(0, 3):
                    print'.',
                for j in range(3, 4):
                    print'o',
                for j in range(4, 5):
                    print'.',
                for j in range(5, 6):
                    print'b',
                for j in range(6, 7):
                    print'g',
                for j in range(7, 10):
                    print'.',
                print''
            for i in range(20, 21):
                for j in range(0, 1):
                    print'.',
                for j in range(1, 2):
                    print'm',
                for j in range(2, 4):
                    print'r',
                for j in range(4, 6):
                    print'o',
                for j in range(6, 7):
                    print'b',
                for j in range(7, 9):
                    print'y',
                for j in range(9, 10):
                    print'.',
                print''
            for i in range(21, 22):
                for j in range(0, 3):
                    print'm',
                for j in range(3, 5):
                    print'r',
                for j in range(5, 7):
                    print'b',
                for j in range(7, 9):
                    print'y',
                for j in range(9, 10):
                    print'.'
                print''
        else:
            return exit()


test3()

同样,我只是专注于获取正确的输出。因此,我现在从句子输入格式应与'p'命令产生的输出格式相同一句中,我需要附加learntris.py以包括test3的代码-位于what.py上。或者,我是否必须将学习代码附加到what.py?我无法在终端中添加内容。我尝试使用learningtris.py> what.py用test 2代码覆盖所有代码,但是它只是删除了所有test3代码,而没有用test2代码替换。似乎我添加任何东西的唯一方法是使用testris.py运行它。
我去的时候:

Again, I was just focused on getting the correct output. So I now from the sentence "input format should be identical to the output format produced by the 'p' command" that I need to append learntris.py to include the code for test3 - located on whatever.py. Or do I have to append whatever.py with the code from learntris.py? I'm having trouble appending in the terminal. I tried going learntris.py > whatever.py to overwrite all that code with test 2 code but it just deleted all the test3 code and didn't replace it with test2 code. It seems the only way for me to append anything is to run it with testris.py. When I go:

./testris.py --shell ./learntris.py >> ./whatever.py 

它导致终端仅打印另一行。只是另一个MacBook-Pro:learntris user $行正在打印。但是,当我检查what.py文件时,我看到从失败的测试3中获得的所有输出仅被添加到测试3代码的末尾。

it results in the terminal just printing another line. Just another MacBook-Pro: learntris user$ line getting printed. But when I check the whatever.py file I see that all the output you get from failing test 3 is just added to the end of the test 3 code. When I go

./testris.py --shell ./whatever.py >> ./learntris.py 

它只是将失败的测试2输出添加到learningtris.py的末尾码。因此,我猜 >>仅用于附加文本输出?还是我在测试3代码的前面缺少某些内容来捕获p命令输出?还有一种更有效的方法来编写测试3的代码吗?

it just adds the failed test 2 output to the end of the learntris.py code. So I'm guessing that '>>' is for appending text output only? Or am I missing something in the front of my test 3 code to catch the p command output? also is there a more efficient way to write my code for test 3?

推荐答案

克隆该项目以检查此问题。

Cloned the project to check this issue.

我想这是一个简单的代码,可以执行程序员期望的前两个任务(尝试使用list编写它是一个很好的练习理解和lambdas(如果您想从该项目中获得更多收益)

I suppose here's a simple code doing what they expected the programmer to do for the first two tasks ( and it'd be a good exercise to try and write it with list comprehensions and lambdas if you want to get more out of this project)

def tetris():

    command = input('')

    if command == 'q':
        exit()
    elif command == 'p':
        for i in range(0, 22):
            output = ''
            for j in range(0, 10):
                if j == 0:
                    output = '.'
                else:
                    output = output + ' .'
            if i != 21:
                print(output + '\n', end="")
            else:
                print(output)


if __name__ == '__main__':

    tetris()

对于第三项任务,您只需添加另一个条件,检查输入是否为'g',预先设置矩阵。

For the third task, you'd just have to add another condition, checking if the input is 'g' and then pre-seting the matrix.

这篇关于发布附加文件python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆