Build a Number Pattern Generator - test 6

Tell us what’s happening:

I can’t pass test 6, integer value. Manually testing the input with negative or character values show that it works. What am I overlooking? Here is my code, which I believe works correctly for its output:

Your code so far

def number_pattern(n):  
    try:int(n)
    except ValueError:
        return('Argument must be an integer value.')
    n=int(n)
    if n < 1 :
        return('Argument must be an integer greater than 0.') 
    strn = ''
    for i in range(1,n):
        strn += str(i) + ' '
    strn += str(n)
    return(strn)
n = input('Enter a positive integer')
print(number_pattern(n))

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Number Pattern Generator - Build a Number Pattern Generator

What does this line do?

  try:int(n)
print(number_pattern(7.1))
> 1 2 3 4 5 6 7

Try adding this at the top of your function:

    print(type(n))
    print(int(n))

What does try:int(n) do? It tests that statement, and if it is not an int it raises an Exception, ValueError which is returned. So that works when the input is not type int. That would be for floats, like 7.1. If it is int, but negative, the next test works. What I don’t understand now is how the constant 7.1 instead of variable n is different. When my code is run and I enter 7.1, the try test works. When I use 7.1 instead of n in the function call, the try fails.

More investigation: It looks like when n begins through an input, it all works. But when n begins as a constant, whether in the call or in an assignment statement, the Try test fails. Why? How do I account for this?

Never mind, I solved it with

if not type(n) == int:
    return('Argument must be an integer value.')

But I still don't understand the issue with a constant, and wonder if this case arises in larger problems

It’s not really testing if it’s an int or not.

It does not seem to do that.

What does this do? int(n)

print(int(7))
print(int(7.1))

What’s n ? Depends what n is.

I removed this for my testing since it’s not in the instructions and you never know if adding other functionality will interfere with tests

n = input('Enter a positive integer')
print(number_pattern(n))

Why not just test the call directly in code?

print(number_pattern(7))
print(number_pattern(7.1))

Thanks for moving me in the right direction

1 Like