Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Need Help Understanding My Python Homework

kemiy

Bronze Coder
Hi everyone,

I’m struggling with some concepts in Python and could use some guidance. I don’t just want someone to do my python homework for me, but I’d really like to understand the logic behind it so I can improve.

The assignment involves working with loops, conditionals, and functions, but I’m finding it hard to connect the pieces. How do you usually approach breaking down problems into smaller, manageable steps?

Any tips, resources, or examples you can share would be super helpful.
 
Last edited:
The rules of structured programming are a good guide in most situations.

5% of the time, I need to break those rules. I'm talking about spaghetti code.

I start with a state transition diagram on paper. A circle represents a program state. Arrows connect states. Conditions are written on the arrows. Important details are written in the circles.

If the language I'm using does not have a GOTO and labels, then I code it like this...
Code:
WHILE state!=0
  IF state=1
    ...
    do something
    test something
    change state variable
    ...
  ELSEIF state=2
    ...
  ELSEIF state=3
    ...
  ...
  ENDIF
ENDWHILE
 
Breaking functions into smaller functions:

There's 101 ways to write a program, and each way has advantages and disadvantages.

I prefer subroutines that are easy to test on their own, and can be used in more than 1 program.

I'd rather have a slow program that re-uses old reliable code, than a fast program that I had to write from scratch.

Lets say I want to perform a calculation on every r sized permutation of n possible numbers.

I do this kind of thing a lot, and I like to structure my routines the same way.
Code:
x = open_p(n, r)
while not eof_p(x)
  y = read_p(x)
Like reading a file. Its not an efficient way to do it, but it is very readable.
 
I've just finished writing a program that solves sudoku grids.

Its huge and ugly, and it took a long time to debug.

Looking at it now, its obvious the way I should have organised my data and structured my functions.

I have coded this before, so there are 2 lessons. The best way to structure a program is never clear at the start, and I need to keep better notes.
 
Back
Top Bottom