Build a Number Pattern Generator - Build a Number Pattern Generator

Tell us what’s happening:

I’ve got the correct output. But why I can’t pass the 3rd, 4th, & the 5th test ?

Your code so far

def number_pattern(n) :
    if not isinstance(n, int) :
        return 'Argument must be an integer value.'
    elif n < 1 :
        return 'Argument must be an integer greater than 0.'
    else :
        number = ''
        for num in range(1, n+1) :
            number += str(num) + ' '
        return number
print(number_pattern(4))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Challenge Information:

Build a Number Pattern Generator - Build a Number Pattern Generator

  • are you adding a space after “last number”?

address that change and try again, happy coding :slight_smile:

Owh, it should be with no additional space ?

Any hints for the change, please ?

how do you check if it is “last number”?

the last number is the n+1

Hi @Fidelis_B_Codes,

If you test like this, you can see that there is an extra space after the last number. What string method can you apply to your final string to remove whitespace from both ends of a string?

print('|'+number_pattern(4)+'|')
print('|'+number_pattern(12)+'|')

Happy coding!

I’m trying using strip but still didn’t work… here’s the updated code so far.

def number_pattern(n) :
    if not isinstance(n, int) :
        return 'Argument must be an integer value.'
    elif n < 1 :
        return 'Argument must be an integer greater than 0.'
    else :
        number = ''
        for num in range(1, n+1) :
            number += str(num).strip()
        return number
print('|'+number_pattern(4)+'|')
print(number_pattern(12))

Hi @Fidelis_B_Codes ! :waving_hand:

Using .strip() is a good idea, but it isn’t implemented in the correct way. You have to add a space between each number, and then delete the last space.

Hope this helped !

Strip the trailing space after exiting the loop.

How about if using .join() and .append() ?