【发布时间】:2012-10-25 19:54:35
【问题描述】:
我对 python 很陌生,把它作为一种爱好来学习,通过一些搜索发现自己有一堆来自“计算实践”的练习,其中一个问关于写作一个 ASCII 数字,如下所示。
这一切似乎都是一个简单的练习,但我似乎无法用数字来画这个,练习表明上面的画是通过使用数字“1”绘制的。
它还指出,任何小于 0 或大于 100 的数字都不能或不应该用于创建 ASCII 绘图。
这是另一个例子:
这里的输入是数字“2”。
我找到了一种让第一张图片出现的方法,但不是通过以任何方式使用给定的数字,只是在 while 循环中使用一个简单的“else”,这样我就可以过滤掉下面的数字或等于 0 或更高或等于 100。
我已经死机了。
如上所述,我的代码不使用变量号来创建第一张图纸:
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 行的问题,因为它需要沿着控制台的侧面表示,它应该与侧面有 0 的间距,但它必须增加到 2 的间距数字 2。
【问题讨论】:
-
我认为如果我们完成整个练习会有所帮助
-
练习陈述如下: 一个大小为 $n$ 的 ASCII 图形由一行或多行组成。在每一行上只允许空格和星号 (*),在一行上的每个星号之后不允许有空格,因此您应该以“\n”或换行符结尾。然后是上述示例。
-
@fp:有关横幅,请参阅Is there a python library that allows to easily print ascii-art text?。但这与当前问题无关
-
@fp 是的,我之前已经找到了这些,但其中一个在“table”e 中有一个看似语法错误,而另一个与我的几乎不一样。但仍然感谢任何帮助或提示。