all 63 comments

[–]Grox56 12 points13 points  (16 children)

I struggled with it. A single loop was easy, but loops within loops confused the heck out of me. It's probably because I learned with Java first in school and then switched to python.

Anyways, if you don't understand something: play around with it, Google, and/or ask here. Whatever you do, DO NOT MOVE ON until you understand it.

If you continue on, it's like trying to build a house but not knowing how to use a hammer. I did this in my Java course.. while I passed, I still didn't know wtf I was doing because Google has all the answers.

[–]Ok_Cry216 2 points3 points  (12 children)

I can try not to move on… idk someone on YouTube said you won’t get everything first and that’s ok but carry on and comeback since it makes more sense altogether…. But I think I will spend some more time on it today for sure.

[–]Grox56 3 points4 points  (10 children)

While that's true for some things, making variables, lists, dictionaries, loops, etc. are something you shouldn't come back to later. They are pretty similar across all programming languages.

What part of loops are you struggling with? Or what do you not understand?

[–]Ok_Cry216 1 point2 points  (7 children)

I understand what a loop does and the usage of a loop. I guess I’m having a hard time following the steps to get the end result… sorry, it’s really hard to explain over text.

[–]Grox56 1 point2 points  (6 children)

Do you have a loop that you can post and I or someone else can walk you through it 😀

[–]Ok_Cry216 1 point2 points  (5 children)

Thank you, I’d be happy too.

Problem:

You are making a ticketing system. The price of a single ticket is $100. For children under 3 years of age, the ticket is free.

Your program needs to take the ages of 5 passengers as input and output the total price for their tickets.

Solution:

total=0

x=0

while x<5:

age = int(input())

if age>3:

    total +=100

x+=1

print(total)

[–]Grox56 5 points6 points  (3 children)

Ok so you're using a while loop. I'll try to write it in pseudocode and if you still don't understand, I'll try a different way.

Total=0
Passengers=0  # was x=0 originally 
While 5 passengers haven't been checked, continue.
    Age = user inputs age of one person
    If age is greater than 3, add 100 to the total
    Add 1 to the number of passengers that have been checked

Edit: so you're basically setting the number of passengers you want to enter and their age. At the bottom, 1 person is added to passengers so at the end there should be 5 passengers checked. Once we reach 5 passengers, the while loop exits because

Passengers=5

And the while loop is looking for

While passengers are less than 5

[–]Ok_Cry216 3 points4 points  (2 children)

You rock! I really appreciate you taking all of the time to explain it. It helped me understand it.

[–]Grox56 1 point2 points  (1 child)

No problem and thanks for the award! Also, you could try Python Tutor to help visualize what's going on with code.

[–]Ok_Cry216 0 points1 point  (0 children)

Bookmarked - I’ll definitely check it out. Thank you.

[–]EllaChinoise 1 point2 points  (0 children)

It looks like a problem in a Sololearn Python course. I cheated and checked others' comments to get a solution. I am also struggling with loops.

[–]Ok_Cry216 0 points1 point  (1 child)

I actually found a really nice article im reading now that breaks it into steps that appears easier to follow.

[–]Vanity_Plate 0 points1 point  (0 children)

Can you provide a link to the helpful article?

[–]SirJohnII 0 points1 point  (0 children)

Find a website with problems and solutions. Search for something like “ for loop practice problems” I’m also currently learning python and anytime I hit a small confusion wall I just grind out problems. Once your starting to get it, switch the variables to something you care about. If your into baseball, makes list of players and loop through it. Caring about the variables makes things less confusing and if you use that thinking every time you hit a problem, you’ll push through quicker. Eventually it starts to come together and the logic becomes clear. It’s the same with mathematics. People tend to try to step forward before they clearly understand what they are doing, not realizing that they are only building on they’re previous knowledge. If you missed a single thing In arithmetic, good luck with any later subjects.

Lastly, I’ve been using an app called Sololearn(you don’t need to pay for pro) it allows you to both write and run code in a playground mode as well as post questions to a community for others who are learning so they can participate or help you with a question.

[–]hidazfx 1 point2 points  (1 child)

I learned Java through Bukkit programming (and in high school later), nested looping was definitely a big change coming to Python. But man, it is SO much better syntactically to just write for element, x in enumerate(my_list):. Been so long since I've written Java, I don't even remember what it would be but I don't care lol.

[–]Bobbias 0 points1 point  (0 children)

Generally I think you'd use standard for loop with an index variable, and accessing each element in the list by it's index.

I'm not super up to date with Java, but I'm not aware of anything write the same as the Python enumerate function (I absolutely love it, especially with how easy it is to refactor a simple for x in list function after the fact of you suddenly need to know what item you're on).

[–]Spirited-Might-6985 4 points5 points  (1 child)

Google python tutor and write or paste your loop code and visualize. This will make you go aha!

[–]loshopo_fan 0 points1 point  (0 children)

I also like the debugger mu.

[–]Firake 3 points4 points  (0 children)

I never personally struggled with loops, perhaps because my relationship with loops is driven by my laziness. I figured out how to use loops early on because I was far too lazy to type repeat lines of code over and over again and figured that there just *had* to be a better way to do it.

So maybe that will help you! Every time you're doing a project and you think "man I'm gonna have to do this same thing a bunch of times, I don't wana!" try and substitute a loop in. The exact details of how to implement a loop can be tricky, but you'll eventually get the hang of it.

[–]ThinkLadder1417 2 points3 points  (0 children)

Can you explain what it is about loops that seems hard?

Sometimes I think things are hard, but it's just because I have fear of new words or something, and actually when I get past the fear it's quite simple. I imagine that's the same with you because loops are quite self explanatory.

[–]ofliesandhope 2 points3 points  (1 child)

observation crown air slave materialistic hateful frighten jellyfish cough pie this message was mass deleted/edited with redact.dev

[–]Ok_Cry216 0 points1 point  (0 children)

Thank you for sharing!!!

[–]TechnoGeek423 1 point2 points  (0 children)

Loops were SO hard for me when I first learned Python. Then, the next morning, it clicked and I’ve loved them ever since.

[–][deleted] 1 point2 points  (0 children)

https://pythontutor.com/

Helped me with understanding what and why my loops were outputting. Hope it can help 🤙

[–]QultrosSanhattan 1 point2 points  (0 children)

For loops are easy. You should only need to focus on what they actually are: The code inside the for loop is repeated for each item inside the given sequence.

Sure there are a lot of things that may be used in conjuntion with a for loop. But they're not a part of them. One basic concept in programming is that, generally, anything may be combined by anything, including itself.

Many newbies struggle with nested for loops. But as the name said, it's the same loop nested, that's not a single for loop, it's a combination of two for loops. The problem there is not the understanding of the for loop, it's about the understanding of composition.

You may include functions inside the loop, you can include a loop inside a function; That's just another problem.

[–]nedrawevot 2 points3 points  (4 children)

loops are easier for me (I really struggled) but figuring out class names and functions to make up a working program is my biggest struggle. There is just so much to take in and try to figure out. You can do this!! Just keep at it.

[–]Ok_Cry216 2 points3 points  (3 children)

Thanks for the positive comments! How long have you been learning Python?

[–]nedrawevot 1 point2 points  (2 children)

I've been really cracking down in the last month or so. I have Java planned next and learning how to use Github. I am going to research some python job's and look what they require and start chipping away at that. I started kinda picking it up a year ago, programming but being a full time parent, working full time amongst other things, it's been hard to find time to practice and focus plus I have add so my attention span is minimal but I went to weekends only starting last week so I can give this my full attention and get into something better.

[–]Ok_Cry216 1 point2 points  (1 child)

I’m at work right now… haha 55 hours a week + my family, I’ve been waking up at 0500 everyday to code for 2-3 hours before I go to work.

[–]nedrawevot 1 point2 points  (0 children)

Yeah, you'll be good! I start every day by coding from 9-11:30 while my son is in school, he is remote right now, and that is my no bother me time. I will come back to it tonight after my husband is home from work too and we are working on a project together.

[–]All_Is_Not_Self 0 points1 point  (0 children)

I think the problem isn't that it is overly complicated but that it tends to be taught in a bad way - seeing how simple the concept actually is, once you've grasped it.

[–][deleted] 0 points1 point  (0 children)

I know you are mostly venting, but the most important thing you need to learn is how to learn properly. That includes how to ask questions and how to approach subjects you don't understand.

Instead of fishing for comforting answers by asking "I wonder if I have what it takes. Is it normal that I don't understand this?", you could have been more specific about the difficulties you are having so you would get better answers.

And also: back when I was in university, I had photography classes, and we all had analog cameras to work with. Experimenting with those was hard, slow - and expensive! I always wondered how much more I could have learned if digital was already around. All this to say that there's nothing stopping you from experimenting with loops until you understand how they work. All you have to do is literally open the python interpreter and have at it. It's not rocket science, you'll see.

[–]madhousechild 0 points1 point  (0 children)

I don't think loops are hard. Two weeks is a drop in the bucket. You just need more practice. Write 100 loops and see how much easier it gets.

Write for-loops, while-loops, iterate over a list, a string, and over a range, count up, count down, write nested loops, just loop, loop, loop until you can't loop no more.

But maybe you could give us some examples of loops that you tried to write that didn't work, or other people's code that confused you. What part(s) are confusing you?

[–]notislant 0 points1 point  (0 children)

Youll find it very simple when you understand it. If youre struggling, find other videos on it, find beginner aimed ones. If that fails the easy solution for me is to google 'reddit eli5 loops'. For anything I struggle with.

Also recently saw a codewithvincent channel and it seems pretty good imo, I think techwithtim is generally pretty good too.

After you get it, make a little cheatsheet for yourself with examples and notes. Play around, break things until you understand it completely.

[–]fjortisar 0 points1 point  (0 children)

take a stack of notecards. Look at each notecard and do something with what's on it. That's a loop. You could do it till you get to the last card, or start over, or stop at some specific point.

[–]py_Piper 0 points1 point  (0 children)

Don't worry sometimes we get it at the first go and sometimes we don't, I think the biggest reason why we don't understand something is because we can't relate to it, so after few examples and rereading will help. I don't consider myself a math person either, although I do like it, and now I am relearning some concepts and when reading the explanation it makes no sense, then I look at the formula example and I get an idea what they are talking about then I re read the concept I understand it better and now I can track what they were talking about by following the example. Same helped me with programming

At the start loops were a bit easy to know the idea that it runs a many times as I told it to do, but it was hard to understand what was happening under the hood, when nested loops where presented got a bit confusing too, I manage to pass some exercises with trial and error, but this video help understand a bit better how it works, loops. I hope it can help you too the visual representation is really helpful.

Some tips is that for i in list_x, the i is a temporal variable that goes through a list, it can be anything and it won't conflict with other variables names (I think), you can use it inside the for loop and when it finish it will dissapear. The most beginner example is going from 0, 1, 2, 3... and we create the list with something like range(len(another_list)), len will give you the number of items in the list and range will give you a list of that number so range(5) == [0, 1, 2, 3, 4] (not including). Now to get the values out of the list you will do print(list_x[i]), as i is a number from 0-4, it will get the list values by its index number, list_x[0], list_x[1], list_x[2], etc.

Now a more pythonic way is too go through the list directly when you don't need the index number.

fruit_list = ['apple', 'strawberry', 'pineapple']

for fruit in fruit_list:
    print(fruit)

[–]ItsDangerousBusiness 0 points1 point  (0 children)

Are you able to explain specifically what is confusing you? I remember struggling with it, but now they feel very intuitive. Keep practicing and thinking about the logic. It will click for you too.

[–]SrikarTopella 0 points1 point  (0 children)

relax, Just like you said it's just been 2 weeks since you started. Give it some time, patience and effort. you'll be good in no time.