0

Number pattern (help to correct my code)

Print the following pattern for the given N number of rows. Pattern for N = 4 1 11 202 3003 My code- n = int(input()) i=1 while i<=n: j=1 while j<=i: print(j,end='') j=j+1 print() i=i+1 On input of 2 Result- My Output 1 12 Expected Output 1 11

6th Jul 2021, 9:21 AM
Student
Student - avatar
13 ответов
+ 2
n = int(input()) if n > 0: print(1) for i in range(1,n): print(str(i)+"0"*(i-1)+str(i))
6th Jul 2021, 9:30 AM
visph
visph - avatar
+ 2
Thank you I have tried this code and it's running perfect. https://code.sololearn.com/cwzsY5Ht5X1u/?ref=app Happy coding!
6th Jul 2021, 10:53 AM
Student
Student - avatar
+ 1
n = int(input()) i=1 while i<=n: j=0 while j<i: print(0 if j and j<i-1 else i-1 or 1,end='') j=j+1 print() i=i+1
6th Jul 2021, 9:38 AM
visph
visph - avatar
+ 1
I've tested both... are you sure to have correctly copyed?
6th Jul 2021, 9:48 AM
visph
visph - avatar
+ 1
updated second one where I made a mistake ^^ do the ide wich you run the code run python3 or python2? in python2, print must not have parenthesis ;)
6th Jul 2021, 10:02 AM
visph
visph - avatar
+ 1
also, I don't know how to specify end parameter in python2 ;P
6th Jul 2021, 10:04 AM
visph
visph - avatar
+ 1
what is your ide and what is exactly the runtime error message (as well as the input provided)?
6th Jul 2021, 10:07 AM
visph
visph - avatar
+ 1
# have you tried the other shorter code and does it produce also error? # else try with: n = int(input()) i=1 while i<=n: j=0 while j<i: if i==1: print(1, end="\n") elif j and j<i-1: print(0, end="\n") else: print(i-1, end='') j=j+1 print() i=i+1
6th Jul 2021, 10:29 AM
visph
visph - avatar
0
@visph It's showing runtime error
6th Jul 2021, 9:46 AM
Student
Student - avatar
0
But ide on which I am running this program is throwning error. However on sololearn your program is running perfect. But for an input of 2 at place of 22 output must be 11. https://code.sololearn.com/cnMJelsbmw9i/?ref=app
6th Jul 2021, 9:56 AM
Student
Student - avatar
0
I am using python 3.5
6th Jul 2021, 10:06 AM
Student
Student - avatar
0
I'm using coding ninjas ide Here is the input and output https://ibb.co/fCFPK72
6th Jul 2021, 10:15 AM
Student
Student - avatar
0
a=int(input('enter the number')) for i in range(a): if i!=1: j=i-1 print(i,j*'0',i) else: print(i) print(i,i)
7th Jul 2021, 4:40 PM
Chandu
Chandu - avatar