(接上一篇)既然能打出三角形星星,怎么打成对称的呢?

Test

Result

triangle_stars(3)

* 1

* * 2

* * * 3

* * 2

* 1

triangle_stars(5)

* 1

* * 2

* * * 3

* * * * 4

* * * * * 5

* * * * 4

* * * 3

* * 2

* 1

n=5;

for i in range(1,n+1,+1): # note the use of the range function, to generate a list equal to [1,2,....,n,n+1]

        for j in range(i):

            print ("* ", end="") #note the use of the end function, which replaces the default /n with "".

        print(i)

Using all your knowledge of loops so far, make the following additions to the above code:

  • wrap the loop in a function triangle_stars(int), that takes an integer input of any positive value
  • fix the bug in the code that causes it to print one fewer rows of stars than needed (eg with the integer 5, it should print 5 rows of stars, from 1 to 5. The code currently prints 4 rows).
  • add another pair of nested for loops within the same function, which print the same thing but flipped, (ie a decreasing number of stars). 
  • finally, at the end of every row, print the total number of stars in that row (no 0's).
  • NB that the middle row of stars should only be printed once

also note that the range function can be run to generate a count down, using the form: range(start,stop,-1), and that it should probably be used to make your upside down triangle.

下一篇揭晓答案

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值