如何在正方形内显示正方形 [英] How to display square within a square

查看:248
本文介绍了如何在正方形内显示正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的程序只使用我定义的函数在正方形内输出一个正方形,但我似乎无法将所有内容排成一行。我已经得到了空心方块和半满的方块,但这个方块在一个方形业务中我无法弄清楚。这是带有示例输出的程序:



I'm attempting to get my program to output a square inside of a square using only the functions I've defined, but I can't seem to get everything to line up. I've gotten hollow squares and half-filled squares, but this square inside a square business I can't figure out. Here is the program with example output:

def star():
""" Display a star without the normal new line """
print('*', end='')

def fill():
    """ Display a fill character without the normal new line """
    print('#', end='')

def space():
    """ Display a space without the normal new line """
    print(' ', end='')

def newline():
    """ Display a new line """
    print()

def squareInSquare(outerSize, innerSize):
    """ Display a small square inside a larger square
        - This example has outerSize = 10, innerSize = 4

    print('Outer and inner square of size', outerSize, 'and', innerSize)





我的尝试:



我不知道如何处理这个问题,我已经学会了如何做一个空心方块和一个填充方块,但我不知道如何将它们组合起来这应该是什么。



What I have tried:

I'm not sure how to approach this, I have learned how to do a hollow square and a filled square, but I'm not sure how to combine them into what this is supposed to be.

推荐答案

引用:

我是不知道如何处理这个,我已经学会了如何做一个空心方块和一个填充方块,但我不知道如何将它们组合成这应该是什么。

I'm not sure how to approach this, I have learned how to do a hollow square and a filled square, but I'm not sure how to combine them into what this is supposed to be.



你正在学习的工作是创建算法,如果我们只给你一个完整的解决方案,你将什么都不学,它将破坏家庭作业的目的。

学习创建算法是通过经验和通过实验,您可以了解哪些是有效的,哪些是无效的。这是第一手的过程,没有人可以为你做。



建议:做一套相关的练习越来越复杂。

- 以'。'打印20x30的游戏区域,使其改为播放场大小

- 添加功能以在给定位置打印星号'*'

- 更改代码以获得更多明星

- 添加功能以在给定位置打印正方形'#'

- 更改代码以获得更多正方形

- 添加功能在给定位置打印矩形'#'

- 更改代码以获得更多矩形

- 添加功能以打印以playdield为中心的方块





为了帮助您理解为什么某些东西不起作用,学习调试器,这是一个不可思议的学习工具。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


The job you are learning is about creating algorithms, if we just give you a full solution, you will learn nothing and it will defeat the purpose of homework.
Learning to create algorithms is done with experience and experimenting, you learn by seeing what is working and what is not. This is a first hand process, nobody can do it for you.

Advice: do a set of related exercises more and more complicated.
- print a playfield of 20x30 filed with '.', make it so tiy can change playfield size
- add feature to print a star '*' at given position
- change code for more stars
- add feature to print a square '#' at given position
- change code for more squares
- add feature to print a rectangle '#' at given position
- change code for more rectangles
- add feature to print squares centered on playdield


In order to help you understand why something don't work, learn debugger, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


使用两个嵌套循环,您可以遍历所有外部方块区域。



在每个位置(行,col)检查你是否在
  • 外面方形边框
With two nested loops you may iterate over all the outer square area.

At each position (row, col) check if you are either on the
  • outer square border
  • 内部方形边框
  • 空白区域


这篇关于如何在正方形内显示正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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