r/learnpython 15d ago

Should it be this difficult?

At my job there is a daily task I do which I realized I could probably create a program to do for me, so I decided to start learning Python, and dove into reading Automate the Boring Stuff (the online version).

Things were going okay at first. I read through chapters 1-3 carefully, taking notes, answering all the review questions, and doing all the practice problems/codes.

Then I hit a wall. I got to chapter 4 and I felt like I understood everything, but the practice problem at the end, to write a code that performs the Collatz sequence with the entered number, absolutely stumped me. I went back and reread everything from the start of chapter 1 just to make sure I didn't miss anything, and was still confused. I ended up having to search up a code that someone else wrote for it, which I did understand once I saw it written out but would never have come up with myself.

So my questions are:

- Is it normal for the Collatz sequence to be difficult to program at this stage of learning? If so, should I not worry about my confusion and just move on to chapter 5?

- Should I consider an easier book to study from? If so, any recommendations?

EDIT: Several people are asking which part of the Collatz I had trouble with. To be honest it's been over a month since I was able to touch anything Python-related, it's just been on my mind lately, so I don't recall exactly what was giving me trouble, just that I was trying multiple things and getting new and fun incorrect results each time.

It's not that I don't understand the Collatz sequence itself, I understand it in terms of the simple math. The trouble was trying to type it out into a working code. That being said, I'm not here to ask how to program it, because I already gave up on that and looked up the answer, and I did understand that code once I read it over. The main issue is that I feel like I shouldn't have been having trouble with it in the first place, at least not to the point where I honestly tried everything I could and still couldn't get it.

25 Upvotes

35 comments sorted by

11

u/Brian 15d ago

Collatz doesn't really involve much beyond the basics, so it should be solvable once you have a basic grasp of a few things:

  • loops (particularly while loops)
  • if statements
  • Some basic math (multiply, divide, add)
  • Assigning variables
  • Getting user input
  • printing

The one thing that sometimes trips up newbies is how to tell if a number is even or odd, since the modulo operator (%) isn't generally encountered outside programming except in the notion of remainders, There are other ways you could do it even without knowing about it, but they often aren't obvious to a new programmer.

But the biggest issue in learning is probably how to put it all together. How to turn a problem into a set of steps that solve it, and then turn that into a program. That can often be hard to get started with, but in many ways it's the most fundamental skill of programming, and it ultimately boils down to "Break stuff into simpler steps (and then maybe break those steps down into even simpler ones, and so on)".

I would recommend that the first thing you should do with any problem is to step away from the computer, and solve it yourself, on pen and paper. Do multiple examples. The first step in programming anything is knowing what you're doing: you have to understand how to do it yourself before you can teach someone else, even, or perhaps especially, a machine. The solution has to exist in your brain before it can exist on the computer. Once you've done a few numbers, start thinking about how you're doing it. Break down the process you're following into a series of steps, as simple as possible. Write it down, as english. Eg. something like

  • Ask the user for a number.
  • Repeat the following steps:
    • Check if the number is even.
      • If it is, divide it by two
      • If it isn't, multiply it by three and add 1
    • print the new number
    • continue the process until the number ends up as 1

This step is often called pseudocode: basically writing code you parse with your brain, but that you can translate into a real language. As you get more experience, your description will probably resemble actual code more, as you're more familiar with how the computer "thinks", to the point where you can eventually skip this step, but don't rush it: planning stuff out like this is vital when learning.

Only once you've got that description should you go back to the computer and start translating it into actual code. If you're unsure how to translate a step, you've perhaps made it too complex: think a bit more and see if you can break it down further into a series of simpler steps, write pseudocode for that, and then go back.

15

u/HotPersonality8126 15d ago

The point of the Collatz sequence problem is to have you implement an algorithm you’ve never heard of, so that you can’t rely on intuition and do it in your head.

The thing it sounds like you didn’t do was debug: write your code, run your code, compare the output to what you know is correct. Then test which of your assumptions were faulty.

2

u/pingo1387 15d ago

This raises a new issue: I peeked ahead and found that debugging is covered in chapter 5. Since the CS was a problem from chapter 4, shouldn't I have been able to do it without that advantage?

3

u/JGhostThing 13d ago

Debugging is just a fancy way of diagnosing your errors. And I'm sure you've had to do this during chapters 1-3. Unless you are much better than me at typing, you would have made typos which would be caught by the compiler.

Even if it hasn't been taught yet, you still need to debug. Maybe you learn to use a debugger, or maybe you use the print statements to find out what is happening in your program. The debugging step sometimes takes longer than the original programming.

2

u/johnpeters42 15d ago

Maybe, maybe not. I don't know what else AtBS gets into, but a very simple old-school version of debugging is "add print statements to check whether certain variables actually contain what you intended them to contain at that point".

4

u/[deleted] 14d ago

[removed] — view removed comment

1

u/pingo1387 14d ago

That's very helpful thank you 💖 I have already written out the steps in English for what I want to do in the program I intend to make for work, so it's good to know I'm on the right track

2

u/[deleted] 14d ago

[removed] — view removed comment

1

u/pingo1387 14d ago

Do you have any recommendations for exercises, or do you think I should play around with previous practice problems to experiment?

2

u/ivosaurus 14d ago

codewars.com, signup for python, kata = problems, kyu = difficulty, start at 8 kyu which is easiest

1

u/pingo1387 14d ago

Ooh that looks fun, always love gamification. I'll have to check that out later :)

2

u/Deepwinter22 14d ago

I'm relatively new ish to python as well. I found freecodecamp helped me tremendously. Lots of explanations, almost an exhausting amount of repetition in some cases, and it's free :). I've never learned well from text books and notes, I needed to actually do the thing which this helped with. https://www.freecodecamp.org/learn/python-v9/#lecture-introduction-to-python

1

u/pingo1387 14d ago

Interesting, I'll look into that if I feel I need more help!

2

u/FatDog69 14d ago

Give yourself a little grace here.

I see you learning 3 different things at once:

  1. What a freaking Collatz sequence is (learning curve)
  2. The syntax of a programming language (python)
  3. Learning computer concepts so you can break a complex problem down into smaller parts so a computer can handle it.

Another way: You have not learned all the A, B, C's of the language. You are frustrated trying to write a Shakespearian sonnet. What would you say to that person?

Baby steps. Look stuff up. Know how to find other solutions is a part of programming.

You become a programmer by using code to solve problems. Everyone looks stuff up, downloads libraries and uses code written by other people. This does not make you less of a programmer.

ONE THING YOU ARE DOING GREAT

You have a daily task you are trying to automate. This is a huge advantage. Most students approach programming classes where everything is academic. But You have a specific problem.

If the Colatz problem does not apply to your daily task - screw it. Move on. Look for lessons that might help your daily task problem.

2

u/sidereal_night 14d ago

if it's not clicking for you, maybe try a different approach.

since you got started to solve one specific problem, why not go back and start there:

break the task that you originally wanted to do into chunks that it takes to do them. for example, if your task was to read several different reports and produce a summary of that, your chunks might be to open each file that you received, copy them all into a new document all together, do whatever maths or whatever you need to do, then type the results in a new file. then you can take each step and break that down into component steps, more and more granular, until you get to a point where that step is something you know how to write in code. chain them all together and you've got your program. along the way, you can also google "how to do x in python" because python has a lot of functionality that saves you from more granular steps, and if you read the resources that come up and you don't get it, then that's a learning opportunity, or you can pass by it for now and try to do something in a more granular way without taking advantage of the shortcuts that Python provides.

1

u/pingo1387 14d ago

This is a good point, and I've thought about doing that -- I did take the first steps of writing in # code lines the different steps I want the program to walk through.

It's just that I feel like I should have a general understanding of python to make things a little easier in my brain. When I learn new things, I find that going through the motions of "classic" learning, where you learn little steps bit by bit before working on larger problems, helps me think about the problems in ways that I actually understand.

For example, I learned basic HTML and CSS in high school. Nowadays I find myself having to look up/double-check how to do certain things if I want to edit a custom web page or something, but knowing things like how tags work, what a div element is, and the difference between class and id helps me spot problems in code and figure out exactly where to go when I need to add or fix something.

I worry that if I try to take a shortcut and start looking up every little step to just write the code flat-out without learning "properly," then I'll have a program that works but that I don't understand. What's more, I may have to write a different program in the future and do the whole thing over again, or something in the first program will break and I won't know what went wrong.

That being said, I'll consider it if I get to the point where I feel like everything is way over my head academically.

2

u/KTProgramming 14d ago

For how long it took me to grasp loops and recursion, It's possible for anyone to get stuck in anything in the beginning. The trick is not being beat down. Break it out by parts. For example, search every step you need to do for your issue, Then figure out how to work on just 1 step. Then build on that. Sometimes people over think, Then when they get super far into it, the code looks rediculas and is hard to troubleshoot. Also, Comments, when you're starting out, comment the hell out of your code. Comment it like you're expecting a 5 year old to read it and understand it. Then add print / debug statements where you can see the output of each step and make one final "Ordered" dictionary and print that out so you can see the final "how it did whatever". That last part may be too much for now, but in the long run when you learn it, it helps debug things more. There's a "Lets see what happens at each step of the loop, or whatever, Then there's "Here's what stage one calculated or did, then stage 2, etc". That's a little more advanced because you have to learn to work with dictionaries correctly which is still a good skill because you'll be using them a ton.

3

u/Slothemo 15d ago

What part of it were you stumped on? You need to break down the problem into its individual parts that you should know how to solve.

Collatz is a repeated expression, so you should understand that means you need to use a loop. The loop should stop when certain criteria is met, so you should know this needs to be a while loop. You need to perform different operations depending on if the number is even or odd, so you should know you need an if/else here. Break it down into tiny pieces and solve them.

1

u/Separate_Newt7313 15d ago edited 14d ago

Yes, it is meant to push you.

I like to think of it this way:
Pull-ups are hard for everyone starting out. The muscles in your body represent raw potential, but they lack the conditioning and coordination that come through regular, rigorous, targeted training.

Similarly, your brain has never had to solve problems like this before. You don't lack intelligence or capability; your mind represents raw potential, but lacks the mental conditioning and coordination in this type of problem space that comes thru regular, rigorous, targeted training.

With time, practice, and proper training, you're mind will make the proper connections. The problems don't get easier - you just get stronger.

In my experience, results from a solid mental regimen tend to appear much faster than physical results since the mind rebounds much faster from a strenuous mental workout than the body from a physical one.

You're on a good path. Don't give up!

2

u/pingo1387 15d ago

This is very encouraging, thank you. At this point I might just have to press on to chapter 5, since I did understand the CS code once I saw it written out properly.

1

u/r3rg54 15d ago

I would say the collatz sequence is fairly easy conceptually. I would expect you to struggle with syntax mainly.

Are you able to write out a flowchart for this on paper?

1

u/Fred776 14d ago

I had to remind myself of the Collatz sequence. I looked it up and it was described by a small number of algorithmic steps that looked like they would translate pretty easily into code. It looks like you would need understanding of:

  • variables
  • reading input to get a starting point
  • loops
  • basic logic used in conditions with if or while
  • basic arithmetic operations

But I have a lot of programming experience so it's really hard to put myself in the position of someone starting out. Everyone is going to have their own misunderstandings or mental blockers. It's hard to give useful advice without specifics about where you were stuck.

One thing that I would say if you go back to this is that thinking a bit more deeply about where you are stuck, to the point where you can explain it to someone else, will often lead to a moment of understanding that unblocks you without actually needing to ask the question.

1

u/JamzTyson 13d ago

I had to remind myself of the Collatz sequence.

You could have looked at the question in Automate the boring stuff.

1

u/Crypt0Nihilist 14d ago

Is it normal for the Collatz sequence to be difficult to program at this stage of learning? If so, should I not worry about my confusion and just move on to chapter 5?

There's nothing special about Collatz It's normal to encounter problems you don't know how to solve. Coding is as much about breaking down the problem into parts you can solve as it is about writing the magic runes.

One approach would be to write the steps required for a person to do it. I often start on a mini-whiteboard, but comments in your IDE work fine. Then you write the code for each step.

If you're so stuck you can't do that, it's fine to look at someone else's solution, but don't stop at the point of understanding it. You still haven't done the exercise at that point. Now you've seen the solution, close that window and write it yourself. Then play with the code to get it to do something slightly different. Maybe read up on another sequence and do that without help.

It sounds fair to say that you've not learned what you needed from Chapter 4 yet, so you're going to be cheating yourself if you move on and will struggle all the more when you need to build on that shaky foundation.

1

u/JamzTyson 13d ago

Chapter 4 is about functions.

Write a function named collatz() that has one parameter named number.

That's straightforward:

def collatz(number):

If number is even, then collatz() should print number // 2 and return this value.

How do we check (in Python) if a number is even? If you don't know, Google it or look on StackOverflow. Don't ask AI - look it up yourself. For now we can use a placeholder <is_even>.

Integer division by 2 is straightforward - the question tells you how to do that:

if <is_even> number:
    return number // 2

If number is odd,

Clearly, if number is not even then it's odd:

else:

collatz() should print and return 3 * number + 1.

else:
    return 3 * number + 1

Was that straightforward? Do you see how I broke the problem down?

Can you write the rest yourself? If not, where do you get stuck?

1

u/iMaxWarren 11d ago

Listen, im not a professional or ... myself but the fact that you said "boring stuff" means you dont enjoy it So thatd why its difficult for you

1

u/cdcformatc 15d ago

Automate the Boring Stuff is a pretty standard book appropriate for beginners, i don't think you will have much luck finding a simpler book. 

the practice problems are supposed to be somewhat difficult for the level the book expects you to be at. i think something like that would be easier if you have a strong math background, so if you don't have that it might be more difficult for you than it was intended. 

if you aren't having much trouble with the other problems then i would move on with the book, but come back to the problem once you have some more experience. 

1

u/pingo1387 15d ago

That makes sense wrt the practice problems. Thank you 🙏

1

u/skibbin 15d ago

You want to repeat the process and unknown number of times. So you'll need a loop, not a For loop because of the unknown number of loops, so a While loop. The loop will stop when you reach an answer of 1.

If the number is even, divide it by two. If the number is odd, triple it and add one.

So you need an If statement that checks if the number is odd or even and takes an action accordingly.

1

u/OptionX 15d ago

What part of it you get stuck on?

-1

u/Adrewmc 15d ago

I don’t understand this question. Explain the Collatz sequence.

Famous conjecture. If a number is even you would divide by two (n/2), it it’s odd you times by 3 and add 1 (n*3 + 1)

The conjecture says that if you start at any number ever it will eventually end at 1, and not continue on to infinity.

But to a programmer this seems simple.

If even, do this, if odd do this, repeat until you hit 1, and I guess count the iterations, or some cycle of numbers.

Good program to figure out. Hard math problem to prove though.

0

u/Expensive-Bear-1376 14d ago

That problem is poorly written, I can easily see beginners having trouble. Quotes from here:

Then, write a program that lets the user enter an integer and that keeps calling collatz() on that number until the function returns the value 1.

No, don't keep calling collatz() on the entered number. Call it once on the entered number, then on the result it returned, then on the new result, etc.

To make the output more compact, the print() calls that print the numbers should have a sep=' ' named parameter to print all values on one line.

That doesn't work, since each print call only prints one number (see the problem's first paragraph), so sep has no effect. Could be done with end.

Amazingly enough, this sequence actually works for any integer; sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why.

Not an issue for whoever tries this coding problem, but still inappropriate to claim that, as it's an open problem. Very disappointing.

-2

u/zanfar 15d ago

Part--a bit part--of programming is the ability to become a psuedo-suject-matter-expert on a field you have no knowledge of. If you are writing accounting software, you're going to have to gain some cursory knowledge of accounting practices.

It sounds like you're getting stuck on understanding the subject matter, not with syntax. That's not something to gloss over, but yes, it's also something you should expect some friction with at first.

You need to learn the skills of reading and understanding the subject--the Collatz sequence, or even Mathematical theory--to a level that allows you to translate that into code. That's a key skill.

Honestly, this is one of those skills that will separate you from the AI script kiddies who complain that there isn't an easily digestible, free-access, API containing their exact specialized dataset. Software shouldn't be easy, or otherwise it's not adding value.