Build a Number Pattern Generator - Build a Number Pattern Generator

Tell us what’s happening:

Objectives 3, 4, and 5 are rejected, and I think I know why but I’m not sure how to elegantly solve it. It doesn’t want an extra space at the end of the list of numbers.

Entering 0.4 gives
Argument must be an integer value.
Argument must be an integer greater than 0.

Entering -2 gives
Argument must be an integer greater than 0.

Entering False gives
Argument must be an integer greater than 0.

Entering True gives
1

Entering any string (in this case, “blah”) gives
Traceback (most recent call last):
File “main.py”, line 12, in
NameError: name ‘blah’ is not defined

Entering any positive integer works just fine.

Your code so far

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

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/142.0.0.0 Safari/537.36

Challenge Information:

Build a Number Pattern Generator - Build a Number Pattern Generator

Hi @LingGeekRain

To help debug, try add an 'x’ to the end of the returned value.

Happy coding

I don’t understand what you mean. Add an x where exactly?

Contenacate the string 'x'on the last line of the function, after the num variable.

1 Like

Got ya. Okay, turning return num into return num + "x" and calling the function with 12 gives

1 2 3 4 5 6 7 8 9 10 11 12 x

Which means I no longer have any idea why objectives 3, 4, and 5 are rejected.

there is a space between 12 and the x, there isn’t a space at the end of the requested output

1 Like

When you use the for loop, You end up adding an extra space at last.

1 Like

Okay, so that is the problem I thought it was. Now to come up with a clever if statement (she said, not feeling particularly clever).

I figured it out, no if statement required, just a slight re-work of the for loop :grin:

so how did turn out to look like? i don’t seem to understand all the explanations given here

please create your own topic to ask questions about the challenge

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.