【发布时间】:2016-03-17 23:58:03
【问题描述】:
过去两天我一直在尝试使用来自用户输入的 while 循环来制作星星金字塔
最终结果的示例如下所示:
*
***
*****
*******
*********
但我什至不确定这应该如何工作。正如我在过去的两个晚上试图解决这个问题一样。
这就是我目前所拥有的:
userInput = int(input("Please enter the amount of rows: "))
count = 1
spacing = 0
actualStars = "*"
numberStars = 0
rows = 0
while(userInput <= rows):
rows += count
print()
while(spacing <= userInput):
spacing += count
print(" ")
while(numberStars <= 0):
print(actualStars)
【问题讨论】:
-
这是家庭作业吗?并且有没有特别说明需要使用while循环?
-
是的,它指定了 3 个 while 循环
-
我先看看你的情况。
-
暂时忘记空格。你可以只打印 1 颗星,然后 2 颗星等吗?
-
提示:在python中,你可以写
a = '*' * 10,它的作用与a='**********'相同。您还可以连接字符串b='X' * 3 + 'O' * 4等价于b='XXXOOOO'。因此,您只需要 一个 循环。所以每一行都会打印一系列空格,然后是另一个开始序列。只需计算您必须在每行上打印多少个。 (当然,除非你的作业特别要求多个循环)
标签: python loops python-3.x while-loop