python输出星号组成的菱形图形的方法:
读入一个整数N,N是奇数,输出由星号字符组成的菱形三角形,要求:????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
第1行1个星号,第2行3个星号,第3行5个星号,依次类推,最后n/2共n个星号,然后在依次递减到n行为一个星号。????????????????????????????????
def printStar(intNum):
s = "*"
spaceLength = intNum
blockCount = int(intNum/2+1)
for i in range(spaceLength):
result = s.rjust(blockCount)
if i >= int(spaceLength/2):
print(result)
s = s[2:]
blockCount -= 1
else:
print(result)
s = s+(2*"*")
blockCount += 1
def oddOReven(intNum):
if intNum%2 == 0:
print("please input a odd num data")
else:
printStar(intNum)
if __name__ == '__main__':
while True:
try:
intNum = eval(input("please input a odd num datan"))
oddOReven(intNum)
except BaseException as e:
print("Please input as 1/2/3... Errorcode:%s" % e)
运行结果:
推荐教程:《python视频教程》
以上就是小编分享的关于python如何输出星号组成的菱形图形?的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!