简体   繁体   中英

Any idea why hackerrank doesn't accept my code even if it prints exactly what is asked

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n Here is the exact question from hackerrank:
https://www.hackerrank.com/challenges/staircase/problem

And here 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(-1,n):
        print(n*' ' + (i+1)*'#')
        n=n-1

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

    staircase(n)

If you are using python3: Try following for the same problem (All test cases should pass):

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    num = n
    for i in range(1,n+1):
        space = num-i
        if space==0:
            print("#"*i)
        else:
            print(' ' * (space-1), "#"*i)

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

You have an extra line of spaces on the top Try this-

import math
import os
import random
import re
import sys

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

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

    staircase(n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM