【问题标题】:Nested loop code to create right triangle in Python在 Python 中创建直角三角形的嵌套循环代码
【发布时间】:2013-11-05 08:42:35
【问题描述】:

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

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

给出的执行方块的代码:

画正方形

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 ''

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

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

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

【问题讨论】:

  • 如果你的代码确实有效,但又想改进,这个问题应该迁移到Code Review;如果它不起作用,请说明您预期会发生什么以及这与实际结果有何不同。请参阅 How to Ask 以获取有关什么是好的解释的提示。

标签: python loops nested geometry


【解决方案1】:

只需将while col &lt;= size: 更改为while col &lt;= row:

这将打印出rowX 的数量。

如果row1,则输出为:X

如果row2,则输出为:X X

如果row3,则输出为:X X X

如果row4,则输出为:X X X X

【讨论】:

  • 太好了,谢谢。到目前为止,我在网上找到了所有困难的例子,我没想到会这么简单。
【解决方案2】:

这里有一些代码:

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

结果:

>>> 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

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

希望这会有所帮助!

【讨论】:

  • 我没有意识到这一点。我会尽量记住这一点,但到目前为止,我所学到的只是使用输入。
  • 如果您使用 Python 2,请使用 raw_input。您的教授可能不认为这是一件重要的事情,但在实际应用中,这可能是至关重要的。
【解决方案3】:

代码:

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)

输出:

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

【讨论】:

    【解决方案4】:
    values = [0,1,2,3]
    for j in values:
     for k in range (j):
      print "*",;
     print "*";
    
    1. 定义数组
    2. 首先开始for循环,对变量j中数组的值进行一个一个的初始化
    3. 开始第二个(嵌套)for循环以初始化变量k中变量j的ranje
    4. 结束第二个(嵌套)for 循环以打印 * 作为分配给 k 的 j 的 par 初始化范围,即如果范围为 1,则打印一个 *
    5. 首先结束 for 循环并打印 * 以表示没有初始化数组

    【讨论】:

      【解决方案5】:
      for i in range(1,8):
          stars=""
          for star in range(1,i+1):
              stars+= " x"
          print(stars)
      

      输出:

      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
      

      【讨论】:

        【解决方案6】:
         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''
        

        【讨论】:

        • 你能解释一下你的答案吗?很高兴在这里看到 bit 的解释。
        • 虽然这段代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的答案添加解释,包括适用的限制和假设。
        【解决方案7】:
        for x in range(10,0,-1):
          print x*"*"
        

        输出:

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

        【讨论】:

        • 虽然这段代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的答案添加解释,包括适用的限制和假设。
        • 如果你想通过 in for 循环传递单个语句,你可以在一行中传递它,就像我在 range(10,0,-1):print x * 中所做的那样*”它使用我们在字符串中使用的操作,即*(字符串上的乘法运算符)来执行重复。它工作正常,作为打印倒置直角traingle的单行代码。
        【解决方案8】:

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

        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
        

        【讨论】:

        • 虽然这段代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的答案添加解释,包括适用的限制和假设。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多