'Find sum of the third square of the matrix

I need to find the sum of the third square of the matrix. That means that there is matrix like

0 0 0 0
0 0 0 0
1 1 0 0
1 1 0 0

and the result must be 4

I think that I manage to find sum of the elements in the 1D list that I'm looking for, but I can't solve this problem with 2D lists
Right now I've made somethig like this

matrix_data(1,[[0,0,0,0],
               [0,0,0,0],
               [1,1,0,0],
               [1,1,0,0]]).

%Start task
%+Test matrix#, -Sum
matrix_main(K,S):-
    matrix_data(K,Ms),
    length(Ms,N),
    matrix_pro(Ms, 1,N,0,S).

%Procedure to go through rows and get sum of each
%+Matrix, +Current row ,+Rows Counter, +Accumulator, -Sum
matrix_pro([R|Rs],Cr,Size,Acc,S):-
    print('    Enter matrix_pro   '),
    row_sum(R, Cr, 1,Size,S11),
    print('   Sum is S11 ' + S11),
    Cr1 is Cr + 1,
    Acc1 is Acc +S11,
    matrix_pro(Rs,Cr1,Size,Acc1,S).

matrix_pro([],_,_,S,S).

%Calculate the sum of each element that we need in a row
%List, +Curent row, +Current index/column in list, +Length, sum
row_sum([],_,_,_,0).

row_sum([H|T], Cr, Index, Size, Sum):-
    Cr>= Size/2,
    Line is Size/2,
    Line =< Cr,
    Index =< Line,
    Index1 is Index + 1,
    row_sum(T, Cr, Index1, Size, S1),
    Sum is S1 + H.

row_sum([_|T], Cr, Index, Size, Sum):-
    print('  Entering with no sum   '),
   % Line is Size /2,
    print(' Houston?? '),
    Index > Size/2,
    print('Aaaaaa'),
    Index1 is Index + 1,
    row_sum(T,Cr,Index1,Size,Sum).

I would really appreciate your help.



Solution 1:[1]

Another possible solution (for square matrix of even order):

%        1 2 3 4 5 6
%      1 . . . . . .
%      2 . . . . . .
%      3 . . . . . .
%   Hu 4 x x x . . .
%      5 x x x . . .
%   N  6 x x x . . .
%        1  Hl

sum3q(K, S) :-
    matrix_data(K, Mat),
    length(Mat, N),
    Hl is N / 2,
    Hu is Hl + 1,
    findall( X,
             ( between(Hu, N, I),
               nth1(I, Mat, Row),
               between(1, Hl, J),
               nth1(J, Row, X) ),
             Xs ),
    sumlist(Xs, S).

Examples:

matrix_data(1, [[0,0,0,0],
                [0,0,0,0],
                [1,1,0,0],
                [1,1,0,0]]).

matrix_data(2, [[0,0,0,0,0,0],
                [0,0,0,0,0,0],
                [0,0,0,0,0,0],
                [1,1,1,0,0,0],
                [1,1,1,0,0,0],
                [1,1,1,0,0,0]]).

Queries:

?- sum3q(1, S).
S = 4.

?- sum3q(2, S).
S = 9.

Solution 2:[2]

Hi my Idea is to put the solving part in a function:

import random

counter_attempt = -1
counter_win = 0
counter_loss = 0


def ask(num1, num2, attempt, loss, win):
    result = str(num1 * num2)
    guess = input(f"How much is {num1} * {num2}?: ")
    if guess == "q":
        print(
            f"Thank you for playing, you guessed {win} times, you gave the wrong answer {loss} times, on a total of {attempt} guesses!!!")
        return attempt, loss, win, True
    try:
        int(guess)
    except ValueError:
        print("Please insert int.")
        return ask(num1, num2, attempt, loss, win)
    if guess == result:
        print("Congratulations, you got it!")
        win += 1
        return attempt, loss, win, False
    elif guess != result:
        print("Wrong! Please try again...")
        loss += 1
        attempt += 1
        return ask(num1, num2, attempt, loss, win)


while True:
    num_1 = random.randint(1, 9)
    num_2 = random.randint(1, 9)
    counter_attempt, counter_loss, counter_win, escape = ask(num_1, num_2, counter_attempt, counter_loss, counter_win)
    if escape:
        break

Is that what you asked for?

Solution 3:[3]

You are generating a new random number every time the user is wrong, because the

    num_1 = random.randint(1, 9)
    num_2 = random.randint(1, 9)
    result = str(num_1 * num_2)

Is in the while True loop.

Here is the fixed Code:

import random

counter_attempt = 0
counter_win = 0
counter_loss = 0

while True:
    num_1 = random.randint(1, 9)
    num_2 = random.randint(1, 9)
    result = str(num_1 * num_2)
    while True:
        guess = input(f"How much is {num_1} * {num_2}?: ")
        if guess == "q":
            print(f"Thank you for playing, you guessed {counter_win} times, you gave the wrong answer {counter_loss} times, on a total of {counter_attempt} guesses!!!")
            input()
            quit() 
        elif guess == result:
            print("Congratulations, you got it!")
            counter_win += 1
            break
        elif guess != result:
            print("Wrong! Please try again...")
            counter_loss += 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 slago
Solution 2
Solution 3