+ 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
22 Antworten
+ 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.
+ 2
Hurray 😸 that's right
+ 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
+ 1
it's because you have too much nested for loops, the break statement will just stop the inner loop.
+ 1
where do I need to add break?
+ 1
After all, this problem can be solved using numpy?
+ 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.
+ 1
Jayakrishna🇮🇳 , the whole idea of the task is that values are entered, and not predetermined. But your code is working👍
+ 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..
+ 1
But for what then line 17?...
+ 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..
+ 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 ...
+ 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...
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..
0
Ok, fine. it remains to figure out how to use numpy to solve this problem🐸
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....
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
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
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
0
you are all so smart! thanks!