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