我对Python还很陌生,只是把它作为一种兴趣爱好来学习,通过一些搜索,我在《计算实践》中找到了一些练习题。其中有一个要求绘制ASCII图像,就像下面所示的图像。
这似乎是一个简单的练习,但我无法理解如何使用数字来绘制它。练习指出上面的图案是通过使用数字“1”绘制的。
它还指出,不能使用小于0或大于100的数字来创建ASCII图形。
这里还有另一个例子:
输入的数字是“2”。
我已经找到了一种方法来呈现第一幅图像,但并没有以任何方式使用给定的数字,只是在while循环中使用了一个简单的“else”,因此我可以过滤掉小于等于0和大于等于100的数字。
我遇到了瓶颈。
我的代码如上所述,并未使用变量number来创建第一个图形:
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" 或换行符结束。然后是上述示例。
我的新代码示例取决于变量输入: 此外,在此代码示例中,当输入为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的增加。
我还有一个问题是第3行,因为它需要在控制台的两侧表示出来,所以它应该从一侧开始没有间距,但是随着数字2的增加,它必须增加到2个间距。