如何制作那样的金字塔蟒蛇..?

时间:2015-10-18 13:22:51

标签: python python-2.7

如何制作这样的节目。???

输入:4

    *
   * *
  *   *
 * * * *

我很想知道如何做到这一点,它整周都在困扰我,但这只是一个额外的信用问题,所以我的老师从未解释过如何做到这一点! :( http://i.stack.imgur.com/qlyGu.jpg

1 个答案:

答案 0 :(得分:0)

我认为尝试这会很有趣,这是我的解决方案:

PROMPT_MSG = "Please enter a whole number, greater than 1"
PROMPT_MSG_ERR = "Oops! Please try again.."

def validate_input(input):
    try:
        assert int(input) > 1
        return int(input)

    except (ValueError, AssertionError) as e:
        print PROMPT_MSG_ERR + "\n"
        main()

def main():
    user_input = raw_input("{0}: ".format(PROMPT_MSG))
    valid_input = validate_input(user_input)

    if valid_input:
        print "{0}*".format(" " * valid_input)
        for i in range(0, valid_input)[1:-1]:     
            print "{0}*{1}*".format(
                (" " * (valid_input - i)),
                (" " * (i + (i-1))),
            )
        print " *" * valid_input

if __name__ == '__main__':
    main()