Python中的ASCII艺术 [英] ASCII art in Python

查看:148
本文介绍了Python中的ASCII艺术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python还是很陌生,出于兴趣爱好而选择它,并通过一些搜索发现自己是"计算实践"中的一堆练习,其中一个询问编写ASCII数字,如下图所示.

I'm pretty new to python, picked it up as an hobby interest, and through some searching found myself a bunch of exercises from "The Practice of computing", one of them asks about writing an ASCII figure, like the one denoted below.

这似乎是一个足够容易的练习,但是我似乎无法用数字画出这个数字,该练习指出,上述绘制是通过使用数字"1"绘制的.

It all seems like an easy enough exercise, but i can't seem wrap my head around the use of a number to draw this, the exercise states that the above drawing was drawn through use of the number "1".

它还指出,不能或不应使用小于0或大于100的数字来创建ASCII绘图.

It also states that no number under 0 or above 100 can or should be used to create an ASCII drawing.

这是另一个示例:

此处输入的是数字"2".

The input here was the number "2".

我找到了一种使第一张图片出现的方法,但是没有以任何方式使用给定的数字,而是在while循环中添加了一个简单的"else",这样我就可以过滤掉下面的数字或等于0或大于等于100.

I've found a way to make the first image appear, but not through any use of the given numbers in any way, just a simple "else" inside a while loop so i could filter out the numbers that are below or equal to 0 and higher or equal to 100.

我已经死定了.

如上所述,我的代码未使用变量号创建第一个图形:

My code as stated above that does not use the variable number to create the first drawing :

while True:
s = input("Give me a number to make a drawing with that is between 0 and 100: ")


if not s.isdigit():
    print ("Error, only numbers will make this program run.")
    continue #Try Again but with a number this time

if int(s) >= 100:
    print ("The number is bigger than or equal to 100 and won't work. \nDo try again.")
    continue #try again

if int(s) <= 0:
    print ("The number is smaller than or equal to 0 and won't work. \nDo try again.")
    continue #try again

else:
    print ("%5s" %("*" *3),"\n"'%5s' %("* *"),"\n" '%7s' %("*** ***"),"\n" '%7s' %("*     *"),"\n" '%7s' %("*** ***"),"\n" '%5s' %("* *"),"\n" '%5s' %("*" *3))


    print ('Want to make another drawing ?')
    continue #make another drawing

锻炼内容如下:

$ n $大小的ASCII数字由一行或几行组成.在每行上仅允许使用空格和星号(*),在一行上的每个星号之后均不允许使用空格,因此您应以"\ n"或换行符结尾.然后是上述示例.

An ASCII Figure of the size $n$ is made up of one or several lines. On each line only spaces and the stars (*) are allowed, after each star on a line no spaces are allowed as such you should end with a "\n" or newline. And then followed by the above stated examples.

我的新代码示例取决于变量输入: 此外,在此代码示例中,将其设置为在输入为1时触发.当我增加输入数量时,我仍然无法放大"整个图形.

    while True:

 A = input("Give me a number to make a drawing with that is between 0 and 100: ")
 b = "***"
 c = "*"
 d = " "


 if not A.isdigit():
        print ("Error, only numbers will make this program run.")
        continue #Try Again but with a number this time

 if int(A) >= 100:
        print ("The number is bigger than or equal to 100 and won't work. \nDo try again.")
        continue #try again

 if int(A) <= 0:
        print ("The number is smaller than or equal to 0 and won't work. \nDo try again.")
        continue #try again


 else :
  range(1,99)
 if int(A) == (1) :
  print ((d *((int(A))*2)) + b,)
  print ((d *((int(A))*2))+ c + d + c,)
  print ((d *((int(A))*0))+ b + d + b,)
  print ((d *((int(A))*0))+ c + d*5 + c,)
  print ((d *((int(A))*0))+ b + d + b,)
  print ((d *((int(A))*2))+ c + d + c,)
  print ((d *((int(A))*2)) + b,)

  continue #try again

但是我仍然有一个问题,即ASCII数字中的空格数量增加",并增加了1到2.

But i've still got a problam with "growing" the number of spaces inside the ASCII figure alongside the increase of 1 to 2.

由于第3行我也遇到了问题,因为它需要沿着控制台的侧面表示,所以它与侧面的间距应该为0,但是必须增加到2的间距数字2.

As i've got a problem with line 3 as well, because it needs to be denoted along the sides of the console, it should have a spacing of 0 from the side, but it must increase to a spacing of 2 with the number 2.

推荐答案

考虑一下1和2之间的区别.尝试手工绘制3和4应该使序列起作用的样子.像那些问题中的一个那样思考它,给您一个序列的开始,而其余的则必须我们去做.

Think about the difference between 1 and 2. Try to draw by hand what 3 and 4 should look like to make the sequence work. Think about it like one of those problems where you are given the start of a sequence and you have to work our the rest.

赞:

0 1 1 2 3 5 8 13

0 1 1 2 3 5 8 13

如果您不立即意识到这一点,那就是斐波那契数列.一旦弄清楚了模式,就可以编写任意长的值序列.

If you don't recognize that right off, it is the Fibonacci sequence. Once you figure out the pattern you can write an arbitrarily long sequence of the values.

考虑一下这个简单的ascii序列:

And think about this simple ascii sequence:

1)

#

2)

##
#

3)

###
##
#

4)是什么样的?

或其他ascii序列:

Or another ascii sequence:

1)

#

2)

 #
# #
 #

3)

  #
 # #
#   #
 # #
  #

什么是(4)?

如果仍然没有意义,请尝试设计自己的一些递归形状,这些形状与您尝试找出的形状有点相似(也许与我的第二个示例类似).现在不必担心如何编码,只需担心输出应该是什么即可.然后查看模式并提出算法.

If it still doesn't make sense, try designing a few recursive shapes of your own which are a little similar to the one you are trying to figure out (maybe something along the lines of my second example). Don't worry about how to code it for now, just worry about what the output should be. Then look at the patterns and come out with an algorithm.

这篇关于Python中的ASCII艺术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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