Build a Number Pattern Generator - Build a Number Pattern Generator

Tell us what’s happening:

My code seems to be passing all the tests but according to the test report I only pass #1 and 2
its generating a space separated string of numbers
It rejects < 1 and non integers
Whats am I missing?

Your code so far

def number_pattern(n):
    numbers = ""  # the number pattern
    
    if isinstance(n, int) == True:
        if n<1:  # n is the high end of the pattern
            print("Argument must be an integer greater than 0.")
            return
    
        for n in range(1,n+1):
            string_n = str(n)
            numbers += (string_n)
            numbers += " "

    else:
        print("Argument must be an integer value..")
        return
    return (numbers) 
print(number_pattern(12))     

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build a Number Pattern Generator - Build a Number Pattern Generator

You seem to be mixing up ‘print’ and ‘return’. Printing something is not the same thing as returning a string.

Thank you JeremyLT. I changed the return statements, but still no joy.

def number_pattern(n):

    numbers = ""  # the number pattern

    

    if isinstance(n, int) == True:

        if n<1:  # n is the high end of the pattern               

            return("Argument must be an integer greater than 0.")

    

        for n in range(1,n+1):

            string_n = str(n)

            numbers += (string_n)

            numbers += " "



    else:

        return("Argument must be an integer value.")

    return (numbers) 

print(number_pattern(4))

Can you say more about how you’re now stuck debugging

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

When I press the “Run Tests” button, it appears to fail 3 tests, though it seems to meet the design objectives expressed in the directions. And thanks for the hint about the back-ticks

Did you try running the function yourself instead of only pushing the ‘run tests’ button? It is hard to debug just by looking at the code

Try this to more clearly see the exact string that you are returning.

print(repr(number_pattern(4)))

Thank you pkdvalis! I added an if statement to eliminate the last space in the string. Then passed all tests! Since it isn’t visible in the output, and they hadn’t introduced the repr() function, I think they should have provided a hint to that effect. They are going to lose people to the frustration.

1 Like

Honestly, there are tons of different ways to build a solution and at some point you’re going to hit frustrations like this. The only way through them is running your code and seeing what happens and trying to fix it and running the code and seeing what happens and…

Coding just is frustrating sometimes no matter what, unfortunately