使用Python创建形状或字母 [英] Creating shapes or letters using Python

查看:200
本文介绍了使用Python创建形状或字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图用Python创建星号H,但我似乎无法弄清楚如何创建垂直线来通过使用我有的代码创建字母H (n):
我在范围(n)中:


  print('*',end ='')
print()

def直接(n):
我在范围内(n):
print '*')

def main():
n = 6
直((n-3)// 2)
横过(n // 2)
直((n-3)// 2)

main()

这是输出:

  * 
***
*

有什么想法?感谢您提前提供任何帮助。

解决方案

您必须一次打印一行。一旦切换到下一行,就不能返回到上一行。所以,这个想法是(例如,对于高度3),在第一次迭代中,代码应该打印星星。它应该更改为下一行('\\\
'),然后打印starstarstar,因为它是中间线。最后,再次在最后一行明星。希望这会有帮助。

  def line(a):
print*,
$ b $ (a-2):
print,

print*,

def middleLine(a):
(a):
print*,

a = int(raw_input(Enter height(odd numbers only only than 3):))
$ (a):
如果我!=(a / 2):
line(a)

else:
middleLine(a )

print


So I'm trying to create the letter H out of asterisks using Python, but I can't seem to figure out on how to create a vertical line to go across in order to create the letter H using the code I have written so far:

def across(n):    
    for i in range(n):
        print ('*', end=' ')
    print()

def straight(n):
    for i in range(n):
        print ('*')

def main():
    n=6
    straight ((n-3)//2)
    across(n//2)
    straight ((n-3)//2)

main()

this is the output:

*
***
*

Any ideas? Thanks for any help in advance.

解决方案

You have to print one line at a time. Once you have switched to the next line you cannot return to the previous line. So, the idea is (e.g. for height 3), on the first iteration the code should print "star star". It should change to the next line ('\n') and then print "starstarstar" since it is the middle line. And finally, on the last line "star star" again. Hope this helps.

def line(a):
    print "*",

    for j in range(a-2):
        print " ",

    print "*",

def middleLine(a):
    for j in range(a):
        print "*",

a = int(raw_input("Enter height (odd numbers only greater than 3): "))

for i in range(a):
    if i != (a / 2):
        line(a)

    else:
        middleLine(a)

    print

这篇关于使用Python创建形状或字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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