0
$\begingroup$

I'm learning Python and I'm having trouble applying logical thinking when trying to solve basic exercises or homework. Every time I try to write code, I end up using random solutions that don't make sense or don't follow Python syntax. I feel like I'm missing the logical thinking part that programming requires.

For example, when I’m asked to write a simple loop or function, I panic and try to guess the solution instead of understanding the structure.

Can anyone suggest specific techniques, resources, or exercises that can help build logical thinking and structured problem-solving in Python?

Thanks in Advance!!

$\endgroup$
3
  • 5
    $\begingroup$ This is hard to answer without more information. Are you in a class or is this independent study? If a class, does the instructor give assignments and then feedback on your results (more than grades)? Does the instructor have office hours? Are you lacking in math? Is this your first programming language? Do you have a book with lots of examples? $\endgroup$ Commented Jun 10 at 19:43
  • $\begingroup$ Pseudocode and flow charts (which are exactly translatable in to pseudocode) teach the structure. Without knowing structures, you can't do very much that is worth doing: cooking, architecture, construction, auto repair... You can't bluff a computer, it is blind and stupid (or, it used to be, now it's even harder to bluff). $\endgroup$ Commented Sep 21 at 11:56
  • $\begingroup$ Could you please edit the question to specify whether you've used other programming languages before? As it stands it's unclear whether you have a problem with python in particular, or with programming in general. $\endgroup$ Commented Oct 7 at 10:12

3 Answers 3

9
$\begingroup$

I doubt anyone can answer the "why?" question without interviews with you, but there's a way to overcome it. Break each programming task into two pieces: solving the problem and writing the code.

First, solve the problem by writing down, in English or whatever language you're comfortable with, the steps the program must take. Do not consider Python syntax, only the steps needed. This is called pseudocode.

When you can step through the pseudocode mentally or with a pencil and get the required result, translate the pseudocode to Python.

$\endgroup$
2
  • 2
    $\begingroup$ +1 if I were asked to give a speech in (say) French, I wouldn't try writing it directly in French. I'd work out what it was I wanted to say. Then I'd draft it in English (my native language) and then I'd translate it into French. Programming is the same, we really need to understand what it is we want to say before we tell the computer about it in a language it "understands". Sometimes we need to step away from the keyboard to make better progress. $\endgroup$ Commented Jul 17 at 16:35
  • $\begingroup$ I agree. Possibly the bigger problem is when one doesn't actually know what the proper steps of an algorithm should be. Programming is not creative, in the sense that the structure of a good solution is not really a matter of creativity, it is from certain knowledge. There are many wrong answers, you must learn the very few right ones to be able to turn them in to code. I see people who think that programming is basically Graphic Design or something. They didn't grow up with text-only terminals. $\endgroup$ Commented Sep 21 at 11:53
2
$\begingroup$

I tackled this issue when I was 14, by trying to write simple text-based print programs which made shapes on the screen: rectangles, triangles, diamonds... Filled in, hollow, random sizes... To make a two dimensional shape you need at least two loops, one nested inside the other. You'll also need some IF statements to control things like filling in a partially hollow shape, accepting valid input (size) parameters and so on.

Try right now to write the steps to form a rectangle printed out with asterisks or something. If you can't even write the steps on paper, start by writing the steps to make one line of the shape. That's your inner loop. Get it working, make it into a Function and write the outer loop which calls it.

In a day or two, you'll learn loops. Then try other kinds of loop structures: they include For, ForEach, While and Until. Originally all loops were IF and Goto. At least understand that as how loops are actually built, you don't need to write them that way unless you want to try.

Understanding is what you need, then you can create from that knowledge.

$\endgroup$
1
  • 1
    $\begingroup$ I'm thinking that someone will ask how they can print text on a screen... We're so far from terminal windows and DOS boxes that people have no clue. It's all beautiful colored shapes now. But you have to program pixels before you can program shapes and colors... You have to start with the basics, which haven't changed, they are just less visible. $\endgroup$ Commented Sep 21 at 12:12
1
$\begingroup$

Stop trying to guess the solution! No amount of guessing will help you improve. Instead, you need to repeatedly practice with code that is NOT for homework. Before you run any program (regardless of who wrote it) on your computer, spend as long time as you need to try to run it by hand on a small input. And then compare your results with the computer. If you get it wrong, figure out what your mistake was, and then try running it again by hand. If you cannot figure it out, use print-statements to help you. You will be able to write programs only after you can run programs yourself.

$\endgroup$
1
  • 1
    $\begingroup$ Yes. All common algorithms were originally done by hand, on paper. You have to program yourself before being able to create programs. You can't do what you don't know how to do. $\endgroup$ Commented Sep 21 at 11:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.