#đź”’ random loop in py question

49 messages · Page 1 of 1 (latest)

cold briar
#

Context:
I'm following this Flask tutorial.. it has little quizzes and such to help you learn; but at times it asks you to finish code. I got stuck here and forced to looked at their solution.

Knowledge cut off:
I don't understand syntax in python loops very well...
I understand the c++/ java loops well

string pets [3] == {cat,dog,chicken};

for(int i=0 i > pets.size();i++){

}

cool

Question:
but here is what i'm trying to understand
Where the heck does 'pet' come from and how do you iterate through them like that.. pets, sure
but for 'pet' in pets.. what?
How does this work?

Question with code:

home.html

{% for pet in pets %}
  <tr>
    <td ><img src= "{{url_for('static', filename = pet["id"]|string+".jpg")}}" height="100" ></td>
    <td>{{ pet["name"] }}</td>
    <td>{{ pet["age"] }}</td>
    <td>{{ pet["bio"] }}</td>
  </tr>
{% endfor %}

app.py

pets = [
            {"id": 1, "name": "Nelly", "age": "5 weeks", "bio": "I am a tiny kitten rescued by the good people at Paws Rescue Center. I love squeaky toys and cuddles."},
            {"id": 2, "name": "Yuki", "age": "8 months", "bio": "I am a handsome gentle-cat. I like to dress up in bow ties."},
            {"id": 3, "name": "Basker", "age": "1 year", "bio": "I love barking. But, I love my friends more."},
            {"id": 4, "name": "Mr. Furrkins", "age": "5 years", "bio": "Probably napping."}, 
        ]
velvet meadowBOT
#

@cold briar

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

chilly marsh
chilly marsh
cold briar
cold briar
chilly marsh
#

We create the var pet in that loop

cold briar
#

woash that's fucking wild

#

so you could say

Pets = […]
for wdjlfkhsdfk in Pets:
    if wdjlfkhsdfk == “Cat”:
        print(wdjlfkhsdfk)
chilly marsh
#

So it goes
Take pets
Slice it up into vars
Assign a var from pets to pet one at a time
Do maths on pet

#

!e
List_of_Vars = [1,2,3,4,5]
print([Var**2 for Var in List_of_Vars if Var > 2])

velvet meadowBOT
#

@chilly marsh :white_check_mark: Your 3.12 eval job has completed with return code 0.

[9, 16, 25]
chilly marsh
#

@cold briar
So I assign a value from the list of Var, check if it’s bigger than 2, then square it, assign it to a new list and then print that new list

#

And var gets created in that function

cold briar
#

that blows my mind a little that you can delcare a variable in the loop with out setting a type

chilly marsh
#

Yeah

#

Python doesn’t require type setting

cold briar
#

i feel like that makes it really difficult to read

#

even if there were a generic keyword like v

chilly marsh
#

Yes… however var names should be clear enough to get rid of that issue

cold briar
#
Pets = […]
for v pet in Pets:
    if pet == “Cat”:
        print(pet)
#

like just that small change would make me feel cool with it-- you can clearly see pet is a new variable of some kind

#

but with out that you're basically reading english instead of code which seems super bad to me

chilly marsh
#

But it is true that there are issues with debugging and finding out that the computer is using a different var type than what you expected

cold briar
#

i think i've found something i don't like about python

chilly marsh
#

XD

#

Ease of use…
Doesn’t mean ease of fix

#

You learn to live with it

cold briar
#

but why don't they make it how i wrote there

#

with the little v

#

isn't that so much more clear

chilly marsh
#

Maybe for you, to me it means you have 2 vars in pets

#

(Lemme show you)

cold briar
#

can you link somwhere wher i can just practice these dumb ass loops syntaxes

#

qq

#

thx btw

chilly marsh
#

!e
Pets = [('Cat', 'Dave'), ('Dog', 'Max')]
for Species, Name in Pets:
print(f'This {Species} is called {Name}')

velvet meadowBOT
#

@chilly marsh :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | This Cat is called Dave
002 | This Dog is called Max
velvet meadowBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

chilly marsh
#

@cold briar but yeah, can’t put v in a for loop as the code couldn’t be sure you don’t mean to have 2 variables

cold briar
little tangle
# cold briar **Context:** I'm following this Flask tutorial.. it has little quizzes and such ...

Your example of a C++/Java loop is broken (missing a ; and wrong comparison), and note that iterating over things isn't some novel Python feature - quite a lot of languages have it, including both C++ and Java.

// https://en.cppreference.com/w/cpp/language/range-for
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (const int& i : v)
    std::cout << i << ' ';
// https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
int sum(int[] a) {
    int result = 0;
    for (int i : a)
        result += i;
    return result;
}
#

it's pretty much only C that comes to mind for not having for-each loops (and C just doesn't have many nice things in general).

cold briar
#

thx

#

i think me missing that ; was a fluke, in the sense i know it goes there, but th rest of the information was very helpful

velvet meadowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.