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 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?
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