why why why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

why why why

Finally the code is working But why does it output the same answer several times? depending on the size of the array https://code.sololearn.com/cp3nFYro1D37/?ref=app For example, you input(9 times): 1 1 1 ... 1 And the program output the correct answer two times, but I need not two times, but one

18th Apr 2020, 9:52 AM
shiryaeva
shiryaeva - avatar
22 Answers
+ 5
I think It's better to separate the loops. if you want to nest them, you should use a function in which you'll replace all the 'break' by 'return False' and 'return True' at the end.
18th Apr 2020, 10:58 AM
John Robotane
John Robotane - avatar
+ 2
Hurray 😸 that's right
18th Apr 2020, 11:19 AM
shiryaeva
shiryaeva - avatar
+ 2
Jayakrishna🇮🇳 we cannot break the outer loop that way because it depends on an 'if' condition in the inner loop. i think the best solution is to separate the loops if you don't want to use a function something like this: https://code.sololearn.com/cWEaQ5pcPCXO/?ref=app
18th Apr 2020, 12:49 PM
John Robotane
John Robotane - avatar
+ 1
it's because you have too much nested for loops, the break statement will just stop the inner loop.
18th Apr 2020, 10:43 AM
John Robotane
John Robotane - avatar
+ 1
where do I need to add break?
18th Apr 2020, 10:54 AM
shiryaeva
shiryaeva - avatar
+ 1
After all, this problem can be solved using numpy?
18th Apr 2020, 11:20 AM
shiryaeva
shiryaeva - avatar
+ 1
do you mean a numpy function which check if a matrix is magic? I don't think so, but you can use some numpy functions to easy the job.
18th Apr 2020, 11:49 AM
John Robotane
John Robotane - avatar
+ 1
Jayakrishna🇮🇳 , the whole idea of ​​the task is that values ​​are entered, and not predetermined. But your code is working👍
18th Apr 2020, 2:16 PM
shiryaeva
shiryaeva - avatar
+ 1
Karlusha You can enter.. If you didnot enter any, or there any error in input, then it uses that default case.. It using of exception handling.. Edit: If you don't want remove that.. It's better, if you Program need a number of input, then it's better to check default cases..
18th Apr 2020, 2:18 PM
Jayakrishna 🇮🇳
+ 1
But for what then line 17?...
18th Apr 2020, 2:20 PM
shiryaeva
shiryaeva - avatar
+ 1
Karlusha I think, You completed about exception handling topic. So I mentioned like that.. First run by giving your inputs.. Next, just run without giving inputs.. Use of exception handling, It's better, if you Program need a number of input, then it's better to check default cases.. So You don't need each time enter values while testing..
18th Apr 2020, 2:26 PM
Jayakrishna 🇮🇳
+ 1
Oh, I understand you. But you know what the problem is? In the task, the matrix is ​​actually 10x10, so it will be difficult to prescribe it in the exception ...
18th Apr 2020, 2:31 PM
shiryaeva
shiryaeva - avatar
+ 1
Karlusha Actually, I think it's better in that to use default case. Is it not difficult to 10x10 =100 inputs each time you execute..? Its upto you how you need to implement your program.. It's an idea only.. So you can try without try catch. You implemented it with n=3, so I just given that example.. You can extend it 10. Or else put there like printing invalid input and exit program.. It help your program more readable to others only...
18th Apr 2020, 3:48 PM
Jayakrishna 🇮🇳
0
The break statement break inner for loop not outer(first) for loop.. for k in range(1, n): here this executing for k = 1,2. So 2 times executing same.. Ident the break to match outer loop to make it execute only once..
18th Apr 2020, 10:27 AM
Jayakrishna 🇮🇳
0
Ok, fine. it remains to figure out how to use numpy to solve this problem🐸
18th Apr 2020, 12:11 PM
shiryaeva
shiryaeva - avatar
0
Using return for all if and else will not result correct solution. Then There is no use of loop, since any one if or else execute break loop so don't get second iteration.... In previous code, to break outer loop use break in outer else part.. Like to match else in line 21, add break in line 58 with identation match. Or just put return instead of break in line 57....
18th Apr 2020, 12:42 PM
Jayakrishna 🇮🇳
0
#John Robotane what I mean like this.. It output only once.. Karlusha Are you looking for like this...? Check this once.. Edited: Added below
18th Apr 2020, 1:12 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 your code outputs only once but I don't think it cheks if the matrix is magic. https://en.m.wikipedia.org/wiki/Magic_square
18th Apr 2020, 1:19 PM
John Robotane
John Robotane - avatar
0
John Robotane i didn't read the comments about magic squire.. I just said about loop break.. Then according code logic, it must print for each row, and diagonals so it repeats.. Now I updated code, according to print result only once... Karlusha https://code.sololearn.com/c3KTQ17wQPrN/?ref=app
18th Apr 2020, 2:12 PM
Jayakrishna 🇮🇳
0
you are all so smart! thanks!
18th Apr 2020, 4:05 PM
shiryaeva
shiryaeva - avatar