在 Python 中创建直角三角形的嵌套循环代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19784772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:37:34  来源:igfitidea点击:

Nested loop code to create right triangle in Python

pythonloopsnestedgeometry

提问by user2955471

Professor gave us a simple code that executes a square and we need to add/change the code to output the right triangle shape as shown below. It's just a simple loop within a loop code, but I can't find tips or help anywhere for creating shapes with Python without the code looking extremely confusing/difficult. I need a simple explanation what to do and why I need to make those changes.

教授给了我们一个执行正方形的简单代码,我们需要添加/更改代码以输出如下所示的直角三角形。这只是循环代码中的一个简单循环,但我无法在任何地方找到使用 Python 创建形状的提示或帮助,而代码看起来非常混乱/困难。我需要一个简单的解释,说明要做什么以及为什么需要进行这些更改。

(Nested loop code to create right triangle in Python)

(在 Python 中创建直角三角形的嵌套循环代码)

The code given that executes a square:

给出的执行正方形的代码:

Draw Square

画正方形

size = input('Please enter the size: ')
chr  = raw_input('Please enter the drawing character: ')

row = 1
while row <= size:
    # Output a single row
    col = 1
    while col <= size:
        # Output a single character, the comma suppresses the newline output
        print chr, 
        col = col + 1

    # Output a newline to end the row
    print '' 

    row = row + 1
print ''

The shape I need to output.....

我需要输出的形状.....

x 
x x 
x x x 
x x x x 
x x x x x 
x x x x x x 
x x x x x x x

Once again, just a simple code explanation, it's an introduction to Python course.

再次,只是简单的代码解释,是Python课程的介绍。

回答by Mailerdaimon

Just change while col <= size:to while col <= row:

只需更改while col <= size:while col <= row:

This will print out rownumber of X.

这将打印出row数量X

If rowis 1the ouput is: X

如果row1输出是:X

If rowis 2the ouput is: X X

如果row2输出是:XX

If rowis 3the ouput is: X X X

如果row3输出是:XXX

If rowis 4the ouput is: X X X X

如果row4输出是:XXXX

回答by aIKid

Here is some code:

这是一些代码:

size = int(raw_input("Enter the size: ")) #Instead of input, 
#convert it to integer!
char = raw_input("Enter the character to draw: ")
for i in range(1, size+1):
    print char*i #on the first iteration, prints 1 'x'
    #on the second iteration, prints 2 'x', and so on

Result:

结果:

>>> char = raw_input("Enter the character to draw: ")
Enter the character to draw: x
>>> size = int(raw_input("Enter the size: "))
Enter the size: 10
>>> for i in range(1, size+1):
        print char*i


x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxx

Also, avoid using inputin Python 2, as it evaluates the string passed as code, it's unsafe and a bad practice.

此外,避免input在 Python 2 中使用,因为它评估作为code传递的字符串,这是不安全的和不好的做法。

Hope this helps!

希望这可以帮助!

回答by Ashif Abdulrahman

you can obtain it by simply use this:

您可以通过简单地使用它来获得它:

size = input('Please enter the size: ')
chr  = raw_input('Please enter the drawing character: ')
i=0
str =''
while i< size:
        str = str +' '+ chr
        print str
        i=i+1

回答by anonymous

 def pattStar():
     print 'Enter no. of rows of pattern'
     noOfRows=input()
     for i in range(1,noOfRows+1):
             for j in range(i):
                     print'*',
             print''

回答by Dimple

for x in range(10,0,-1):
  print x*"*"

output:

输出:

**********
*********
********
*******
******
*****
****
***
**
*

回答by Ashish Gupta

Code:

代码:

def triangle(i, t=0):
    if i == 0:
        return 0
    else:
        print ' ' * ( t + 1 ) + '*' * ( i * 2 - 1 )
        return triangle( i - 1, t + 1 )
triangle(5)

Output:

输出:

* * * * * * * * *
   * * * * * * *
     * * * * *
       * * * 
         *

回答by Ujjawal Khare

values = [0,1,2,3]
for j in values:
 for k in range (j):
  print "*",;
 print "*";
  1. define array
  2. start first for loop to one by one initialize values of array in variable j
  3. start second (nested) for loop to initialize ranje of variable j in variable k
  4. end second (nested) for loop to print * as par initialized range of j assigned to k i.e. if range is 1 then print one *
  5. end first for loop and print * for no of initialized array
  1. 定义数组
  2. 先开始for循环,逐一初始化变量j中数组的值
  3. 启动第二个(嵌套)for 循环以初始化变量 k 中变量 j 的范围
  4. 结束第二个(嵌套)for 循环打印 * 作为分配给 k 的 j 的 par 初始化范围,即如果范围为 1,则打印一个 *
  5. 先结束循环并打印 * 表示没有初始化的数组