颠倒python中的星号三角形 [英] reverse upside down asterisk triangle in python

查看:866
本文介绍了颠倒python中的星号三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用嵌套的for循环向用户询问一个整数,然后程序将输出一个倒置的倒置三角形,其基数有星号并正在下降。
应该看起来像这样:

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

我的代码是:

  def pattern():
integer = requestInteger(请输入数字)
在范围内(0,整数):
范围内的变量(整数,0,-1):
如果变量 - 1> ($'$')
sys.stdout.write('') )

输出:

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

我不确定如何改变我的循环来完成这项工作,而且我一直在尝试一段时间,所以帮助会非常感谢。谢谢

解决方案

我的采访就像这样:

<$ p
整数= int(raw_input(请输入一个数字))
为范围内的数字(0,整数):
打印*数字+**(整数 - 数字)

是必须的)?提示: * 运算符可轻松替换为 for 循环。


I am trying to use nested for loops to ask the user for an integer and then the program will output a reverse, upside down triangle, with the base of it having the number of asterisks and working its way down. It's supposed to look like this:

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

The code I have:

def pattern():
  integer = requestInteger("Please enter a number")
  for number in range(0, integer):
    for variable in range(integer, 0, -1):
      if variable - 1 > number:
        sys.stdout.write(' ')
      else:
        sys.stdout.write('*')
  sys.stdout.write('\n')

Outputs this:

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

I'm not really sure how to go about changing my for loops around to make this work, and I've been trying for a while, so help would be much appreciated. Thank you

解决方案

My take would be something like this:

def pattern():
    integer = int(raw_input("Please enter a number"))
    for number in range(0, integer):
        print " " * number + "*" * (integer - number)

Is this homework (nested loops are mandatory)? Hint: the * operator can be easily replaced by a for loop.

这篇关于颠倒python中的星号三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆