Home > Blockchain >  what is the error of this staircase problem in Hacker Rank?
what is the error of this staircase problem in Hacker Rank?

Time:06-17

This is my code:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(0,n):
        for j in range(0,n):
            if (i   j >= n):
                print("#",end='') 
            else:
                print(" ",end='')
        print("\r")

if __name__ == '__main__':
    n = int(input())

    staircase(n)

Sample input: 4

Sample output:

   #
  ##
 ###
####

This says there is Out Of List error. How do I have to solve it?

CodePudding user response:

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(1, n 1):
        print(" "* (n - i)   "#" * (i))

n = 4

staircase(n)

CodePudding user response:

You can do it with for-loop and each iteration print one-space and one-sharp base each iteration: One line:

n = 5
print(*[' '*(n-i)   '#'*i for i in range(1,n)], sep='\n')

Expand version:

def prnt_shrp(n):
    for i in range(1,n 1):
        print(' '*(n-i)   '#'*i)

Output:

>>> prnt_shrp(5)
    #
   ##
  ###
 ####
#####

Explanation:

>>> '#'*3
'###'
  • Related