r/learnpython • • Jan 05 '24

I can't seem to code properly.

Hey everyone.

I started learning Python from yesterday itself, and it's very intriguing. I've pulled all nighters to watch MIT lectures and Corey Schnafer. As of now, I can safely say I understand how to work with strings, numbers, tuples, and lists. That to a slightly basic extent. Today, I decided to go a bit adventurous and tried learning to code a Tic-Tac-Toe. Didn't work. Looked up code online - I barely understand any of it.

I know you may think, "This guy is a laugh! He's running before he could walk!" Thing is, things seemingly come natural to me, especially Mathematics. And, programming languages such as these are very mathematically-oriented. So, I felt a bit disheartened.

I scrolled on this subreddit and found that many people watch lectures and read books, but when it comes to the coding aspect of it, they frazzle. They don't know what to do. I want to prevent that. But how? I don't want to code print("Hello World!") forever. Any advice? Any tips? If you could share your personal anecdotes, please do. Apparently, there is some imitation culture where you can copy other people's codes and tweak it a bit. Is that ethical ?

Also, once I finish learning the basics and experimenting and coding up basic projects, I plan on learning pandas and mathplotlib (I am interested in Data Science) and some Django.

2 Upvotes

12 comments sorted by

View all comments

42

u/hornetjockey Jan 05 '24

Congrats. You are a gifted person that has come up against an actual challenge. I think if you try to skip ahead just assuming you’ll get it, you’re going to have a bad time. Crawl, walk, run. A good first game is “pick a number between 1 and 10”.

8

u/[deleted] Jan 06 '24

I'm one of the so-called "gifted" people. I wish I wasn't. Yeah, it's great when you can do no homework or studying and ace your tests because of the fact that they're multiple choice. Throw anything on a multiple choice test; if you've got logic and some common sense you'll probably ace it easily.

The problem is that life isn't a multiple choice test. The options aren't given to you in a list where you choose one. Life is one giant long answer essay where you don't even know what the question you're answering is.

Same with coding. There are tons of different ways to implement whatever project you're working on. When you're new, you're also at the first level of competence: unconscious incompetence. Essentially, you're bad at it, and you don't know that you're bad at it or why. You don't know what you don't know.

The only way to get better is obvious. Code more. Ask LLMs questions about how to do different things, and have them expand on every term or function they use that you don't understand. Test their code (they write buggy code often). Write your own code that does the same thing in a different way. Start small.

Tackling a whole game is a large task. Break it down into steps. For example, write a function that takes a one dimensional array of 3 elements in length, and returns true if all the elements are the same, or false if they are all not the same. Remember that all the elements being empty does not constitute a win for anyone, so you have to handle that.

Then later as you're building your tic-tac-toe game, you can use this function over and over as you're checking the board for a win state. Slice the 2D array representing the board into 1D arrays representing the rows/columns/diagonals (of length 3), and feed each one into that function you just wrote. Depending on whoever just moved, you'll know who wins when this function comes back with "true".

^ That's just one possible way to implement a check for a win state.

Happy coding.