0

My code for printing a matrix

My questions are- 1. how do i access strings which the user has typed in this html file 2. is my loop correct?

1 Answer 1

1

set belongs into {% %}, not {{ }}, as it's a statement. The different kinds of curly braces combinations are basically on the first page of http://jinja.pocoo.org/docs/2.9/templates/ .

Missing ) in range(len(string2)).

I have no idea what i in this context is meant to do.

I also don't think i += 1 might work in any way.

You use {{% endfor %}} instead of {% endfor %}.

Missing {% endfor %} for the second for.

You use len(string1) all the time, shouldn't it be string2 for the columns? (or the other way around, but one for the rows and one for the columns)

What's the render_template line, do you pass all those variables to the template?

Assuming you did like render_template(..., string1=someString, string2=someOtherString, matrix=someListOfLists), both

{% for col in range(len(string2)) %}
    <td>{{ string2[col] }}</td>
{% endfor %}

and

{% for letter in string2 %}
    <td>{{ letter }}</td>
{% endfor %}

should do about the same thing.

If I interpret your code correctly, something like

{% for letter in string1 %}
    <tr>
    <td>{{ letter }}</td>
    {% for elem in matrix[loop.index0] %}
        <td>{{ elem[0] }}</td>
    {% endfor %}
    </tr>
{% endfor %}

should work for the table body, using iteration over list elements rather than indices (well, using the index of the outer loop once). But your approach (when using the right markup, as written above) should work, too.

1
  • Thanks, the variable i is meant to print the a header row from 0 to 1. I forgot the loop. The render_template passes a list of args, so those are somehow passed to jinja as s1 and s2. now i just need to figure out the loop .
    – pj8126
    Commented Mar 9, 2018 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .