简体   繁体   English

Python 3.43:制作网格并使用sys.stdout.write()

[英]Python 3.43: making a grid and using sys.stdout.write()

I understand this has most likely been asked before, I am just hoping to get some insight after hours of searching. 我知道这很可能是以前问过的,我只是希望在经过数小时的搜索后能获得一些见解。 I am attempting to make a grid using python 3.43. 我正在尝试使用python 3.43制作网格。

+ - - + - - +
|     |     |
|     |     |
+ - - + - - +
|     |     |
|     |     |
+ - - + - - +

This is my code thus far, and I successfully have the first 4 columns and 7 rows. 到目前为止,这是我的代码,并且我成功拥有前4列和7行。 The trouble is extending this to be a pattern, to make two more connecting boxes to make a grid. 麻烦在于将其扩展为一种模式,以使另外两个连接盒成为一个网格。

print ('#1')
corner = '+'
bar = ' -'
side = '|'
space = ' '

top_row = corner + bar + bar + space + corner
sides = side + space*5 + side

def do_twice(func,arg):
    func(arg)
    func(arg)

def sides(width):
    sides = side + (space * width) + side
    print (sides)

def top(length):
    top_row = corner + (bar * length) + space + corner
    print(top_row)


top(2)
do_twice(sides,5)
top(2)
do_twice(sides,5)
top(2)

This gives me one side of the grid. 这给了我网格的一侧。 To those who are experienced in python, it may seem rudimentary but I'm just trying to grasp the basics. 对于那些有python经验的人来说,这似乎很初级,但我只是想掌握一些基础知识。 What I have tried to do to add the other side for the top row is: 我试图为第一行添加另一面的方法是:

import sys
def top(length):
    top_row = corner + (bar * length) + space + corner
    print(top_row),(sys.stdout.write(' - - +'))

It actually helps, but it returns "None" at the end, and I beleive this is because I am essentially printing twice, but my intention is to just add on text to the same line. 它实际上有帮助,但是最后返回“ None”,我相信这是因为我实际上要打印两次,但是我的目的是将文本添加到同一行。 The result is this: 结果是这样的:

+ - - + - - + None
|     |
|     |
+ - - + - - + None
|     |
|     |
+ - - + - - + None

Anyone have any ideas as to how I can either extend this function or another method to print on the same line? 任何人都对如何扩展此功能或另一种方法在同一行上打印有任何想法?

I really appreciate it, and thank you for your time. 非常感谢,感谢您的宝贵时间。

Try: 尝试:

left = corner + (space + bar)*length + space
top = left + corner + left[::-1]
print(top)

The [::-1] reverses the string and the left half of your grid is the reflection of the right half, but with a corner in the middle. [::-1]反转字符串,网格的左半部分是右半部分的反射,但中间有一个角。

You were right with about why you had a None in there. 您正确地知道为什么在那里None I recommend against using print and sys.stdout.write in the same place since they're the same thing and would make your code unnecessarily confusing. 我建议不要在同一位置使用printsys.stdout.write ,因为它们是同一回事,这会使您的代码不必要地引起混淆。

There should be one-- and preferably only one --obvious way to do it. 应该有一种-最好只有一种-显而易见的方法。

-The Zen of Python, Tim Peters -Python的禅宗,蒂姆·彼得斯(Tim Peters)

In Python 3.x, print() takes an 'end' parameter that tells the function what to print after it has printed all it's arguments. 在Python 3.x中,print()带有一个'end'参数,该参数告诉函数在打印所有参数后要打印的内容。 The parameter defaults to '\\n', so that the function moves to the next line after printing. 该参数默认为“ \\ n”,以便函数在打印后移至下一行。 If you want to keep printing on the same line, simply use end=''. 如果要保持在同一行上打印,只需使用end =''。

print('1234', end='')
print('ABCD')           #  should print 1234ABCD

Regarding making a grid, you seem to know that multiplying a string by an integer repeats the string (eg, bar * length => ' - -').In Python 3.x, print() takes an 'end' parameter that tells the function what to print after it has printed all it's arguments. 关于网格,您似乎知道将字符串乘以整数会重复该字符串(例如,bar * length =>'--')。在Python 3.x中,print()带有一个“ end”参数,该参数告诉该函数打印完所有参数后要打印的内容。 The parameter defaults to '\\n', so that the function moves to the next line after printing. 该参数默认为“ \\ n”,以便函数在打印后移至下一行。 If you want to keep printing on the same line, simply use end=''. 如果要保持在同一行上打印,只需使用end =''。

print('1234', end='')
print('ABCD')           #  should print 1234ABCD

About making a grid: You seem to know that multiplying a string by an integer repeats the string (eg, bar * length => ' - -'). 关于制作网格:您似乎知道将字符串乘以整数会重复该字符串(例如,bar * length =>'--')。 You also seem to recognize that you can extend the line by adding ' - - +'. 您似乎还认识到可以通过添加'--+'来扩展行。 Put these ideas together and you get: 将这些想法放在一起,您将获得:

cell_width = 2
grid_width = 3

hpart = bar*cell_width + space + corner  # -> ' - - +'
hline = corner + hpart*grid_width        # -> '+ - - + - - + - - +'

The same technique can be used to make the vertical lines: 可以使用相同的技术制作垂直线:

'|     |     |     |'

and then a row part: 然后一行:

'|     |     |     |\n'
'|     |     |     |\n'
'+ - - + - - + - - +\n'

and then the grid is hline + row_part*grid_height. 然后网格是hline + row_part * grid_height。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM