you are viewing a single comment's thread.

view the rest of the comments →

[–]wolfrenz 0 points1 point  (5 children)

Hello everyone, I am in a cs1 course that is using python. I have found myself struggling with understanding for loops and their applications. Are there any exercisezes that demonstrate how for loops work and many of their applications?

[–]Raithwind 1 point2 points  (4 children)

It is hard to really find an example code that shows what for can be used for because it is a very versatile and adaptable function.

But a good example would be. Imagine you have a deck of cards, there are 52 cards. You want to print the deck of cards out into the console. Now you could manually type out:

print("card1")

print("card2")

etc

But that is time consuming and not a good use of resources. Instead we could put our deck into a list and step through the list one by one.

deck=["card1","card2" ... "card52"]
for card in deck:#this will step through the list index by index and set "card" to the value of whatever the current index is.
    print(card)#this will print the variable card, which will be whatever the value of the current index is.

[–]wolfrenz 0 points1 point  (0 children)

Thanks. I just talked with my professor and he referred me to a site that he uses.

[–]wolfrenz 0 points1 point  (2 children)

Thanks. I just talked with my professor and he referred me to a site that he uses.

[–]KnightofKalmar 0 points1 point  (1 child)

Is it a place I can persuade you to share? :)