我目前在一个初学者python类,所以不要bash太辛苦....但我做基于随机生成的数字的文本模式,目前我面临一个问题,我不能弄清楚
if random_num > 0:
for j in range(random_num):
for i in range(random_num):
print("*"*(random_num-i))
print()
是我的代码的一部分,它创建了一个基于已经生成的随机数的三角形图案。我运行这个命令的结果是:
Do you wish to print another pattern (y/n)? y
Random Number: 4
****
***
**
*
****
***
**
*
****
***
**
*
****
***
**
*
````````````````````````````````````````````````````````````````````````````````````````````````````````
it prints the triangle how I want it by taking one off after every row but as you can see it prints itself same amount of times as number generated. anyone have any imput? also I cannot use "break"
你不需要两个for循环。
一个循环将打印n行,其中n为random_number。每一行将有n - i个星号,其中i是该行的索引——这是因为您将星号乘以(random_number-1)。因此,第0行将有n - 0 = n颗星,然后下一行将有n - 1颗星,以此类推。
random_num = 4
while random_num > 0:
print('*'*random_num)
random_num -= 1
把while循环看作if语句,它不断重复循环,直到条件不满足为止。