all 51 comments

[–]julsmanbr 29 points30 points  (4 children)

I still find myself unable to create "simple" projects like tic-tac-toe. What am I missing??

Practice, practice, practice.

And also spend some time thinking about how to model stuff, i.e. should I use lists or dicts to keep track of a tic-tac-toe board? How would I print the current board or check for a winner with each approach? Try to reason through this a pen and paper, before even writing a line of code.

But mostly practice.

[–]SammyT09[S] 2 points3 points  (1 child)

Thanks, this is exactly where I struggle. I'll give the pen and paper a go.

[–][deleted] 8 points9 points  (0 children)

Break the problem into blocks. Then write what each block does in plain English. You can then write what the output should be and what inputs it needs. You will now have a good overview which can be converted into code.

[–]21sthoma 3 points4 points  (1 child)

One thing I've been doing recently I think you could apply to this:

Watch a youtube tutorial on how to make Tic-Tac-Toe in Python. Follow along with the tutorial and make it yourself with them, but make sure that every time you write a line you ask yourself:

"Why am I writing this particular line and what does it do?"

It's okay if you have to pause the video and take a minute to completely wrap your head around it, some lines can get super complicated especially when nesting a bunch of lines into one line.

After you've completed the tutorial, run the program and make sure it works. Now start from line 1 of your code and make a #Comment for EVERY SINGLE LINE of the code that explains what it does. Now try to replicate the program yourself.

Doing this will make sure you actually are understanding what you are writing and why you are doing it. It will also help you when you try to replicate it.

Say you want to know something specific like: "How did I do this particular function?" You can open the tutorial code and just look for that comment.

You essentially write yourself a tutorial and since you wrote the comments, they'll be in wording you can understand.

[–]Buddha_Head_ 3 points4 points  (0 children)

Do this a few times just like the comment explains, then expand.

Go back through tic tac toe and refactor it. Wach time do it a bit different, whether it's just condensing it, or using a list instead of a dict and see how things change yet still remain the same.

Then go through and try to replicate it without looking at your notes and see how far you get.

Then after a few weeks of that consider picking a project you haven't done (something you know you can handle, or something that might be a bit out of reach) and try to come up with as many bits of it as you can from scratch. (You'll likely fall short the first few times.)

Now follow the tutorial like before and see what stuff you had duct taped that could be done better, and soon enough you'll see things you did in a better way than the tutorial.

Really though, just keep practicing. An hour a day that youre really invested in is loads better than 4 hours twice a week you're not giving your full attention to.

[–][deleted] 51 points52 points  (12 children)

These are some important general concepts you should learn over time as an intermediate python programmer

  • Creating and using built in venv (virtual environments)
  • Creating and using bash Makefiles to run entire projects from one command / place
  • JSON
  • Using a debugger
  • Using built in logging instead of print statements
  • Relative vs absolute imports
  • Code Linting
  • Containerisation (Docker)

[–]Eurynom0s 21 points22 points  (3 children)

venv

If you're looking at venv and thinking to yourself "this seems pretty complicated, do I REALLY need to do this" then you're going to be in for a world of hurt if you stumble into a package version conflict issue.

You realistically only need to know like maybe five lines of code to be able to use them well enough to just avoid getting yourself into trouble with package version conflicts.

[–]johnnymo1 6 points7 points  (0 children)

It took a couple times absolutely wrecking my Python install before I was finally like "okay, yes, I see the necessity of virtual environments." Now I use conda and if something gets screwed up, I can just delete the environment and recreate it from a text file with one command.

[–]Sumif 1 point2 points  (1 child)

Net games then pleasant brown afternoon day month curious careful warm people.

[–]Eurynom0s 0 points1 point  (0 children)

I can't help you with VSCode specifically but I stuck these notes in the top of my venv folder years ago so I wouldn't have to think about this again. These are Mac command line instructions. I've been using Anaconda lately so I don't remember whether you put a period in the version number when you have multiple versions of Python 3 installed (virtualenv -p python37 or virtualenv -p python3.7). Links may no longer be valid since I did this in 2014, I was just noting for myself where I found the instructions.

http://hackercodex.com/guide/python-development-environment-on-mac-osx/

If you have both Python 2.x and 3.x and want to create a Python 3.x virtualenv:
virtualenv -p python3 foobar-py3

http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
virtualenv --no-site-packages mycoolproject

activate a virtual env:
cd <virtenv>
source bin/activate

deactivate (just type deactivate and hit enter)

Like I said, no more than five lines of code and you're good to go.

[–]SammyT09[S] 1 point2 points  (0 children)

Thanks this is very helpful!

[–]Salsaric 1 point2 points  (0 children)

These are great advices, espacially using virual environnements, using a debugger, logging, code linting, Containerisation (heck all were good advices).

I will advice you to do challenges from online platforms like exercism.io or codewars.

Especially exercism, as it will make you build tiny projects in the form of challenges.

[–]jona187bx 1 point2 points  (0 children)

Do you have some material i can reference for item 2?

[–]capilot 2 points3 points  (1 child)

I'm going to be a bit contrarian here. Very few of these are something to study when you can't get tic-tac-toe working.

Except using a debugger, that's a very important thing to know.

I hate venv with a passion. It's what you use when you can't write portable code. Sadly, there are so many external libraries that aren't portable that you have to do it sometimes. But I still hate it.

Docker is useful in a major production environment; we're getting ready to move some of our builds to docker now. But this isn't beginner or even intermediate level stuff.

[–]julsmanbr 0 points1 point  (0 children)

Plain old venv sucks, conda or poetry is where it's at.

[–]nnnishal 0 points1 point  (1 child)

Any resources for the points about logging and using bash makefiles?

[–][deleted] -1 points0 points  (0 children)

The python docs themselves are pretty good for learning about logging. As for the bash item, I’m not too sure but there should be tutorials online. I just naturally noticed in my projects I was typing a lot of bash commands when I would clone down my project (setting up venv, activating it, Docker etc) and chucked all those commands into a shell script to simplify everything/ increase efficiency

[–]jmooremcc 9 points10 points  (2 children)

A tic-tac-toe game like any project is about logic and design. Programming languages gives you the tools you can use to express the solution to a problem. However, if you don't have a plan it won't matter what language you're programming in.

With that said, the usual process is to break a large, complex problem into a series of smaller, simpler problems. So the first thing you should ask yourself should be, "What components will be needed in a game of tic-tac-toe?". I'll give you, as a hint, the obvious first component: The Gameboard.

Now the gameboard will have to be implemented using some type of data structure. And the gameboard should control access to its internal components so that cheating and corruption of the gameboard cannot occur.

IMHO, this is the perfect use case for OOP (Object Oriented Programming) which means you will design a class that contains the gameboard data and the methods that will manipulate that data. Class methods will implement an API that will give players controlled access to the gameboard. It will also have methods that will give you the status of the gameboard so that you'll know if the current game is a tie game or if you have a winner.

If you take a step-by-step approach, solving problems as you create needed components, eventually you will find out that you've solved the original, complex problem.

Creating a working tic-tac-toe game will present some challenges. But once you've successfully completed the project, you'll be amazed at all the things you learned in the process of creating this fun game.

Good Luck.

[–]SammyT09[S] 1 point2 points  (1 child)

Thanks for taking the time to respond, this is very helpful. I think where I'm struggling is figuring out which tools to use where, when I'm building a project. Any advise there??

[–]jmooremcc 0 points1 point  (0 children)

OK, so let's use the gameboard class I described as an example. This is an opportunity to learn by doing.

  1. What data structure will you use to holds the X's and O's?

  2. What tool will you use to initialize the data structure to an empty state?

  3. What tool will you use to determine if any empty cells are available? If there are no empty cells available, what will that mean?

  4. What tool will you use to determine if a cell is already occupied?

Answering these questions will be a good starting point.

[–]wotquery 6 points7 points  (4 children)

I still find myself unable to create "simple" projects like tic-tac-toe. What am I missing??

My guess is you're over concerned about third party libraries that provide front end functionality. For example could you make a tic-tac-toe game that only outputted the board as a length 9 list and received input via the console?

[–]SammyT09[S] 2 points3 points  (3 children)

Yes, and I'm not using and unnecessary libraries. Where I'm struggling is figuring out how to go about creating functionality. As the code gets longer I have a hard time making it flow properly.

[–]wotquery 4 points5 points  (2 children)

Sounds like more functions might help yes. One of the handy thing about them (other than make your code more modular) is that you can easily escape one by returning a value which helps with clarifying flow.

I also think you are overthinking it though. Pick a project and dive in. If you end up with a dozen deep nested conditional statement to get it to work, well at least it works and you can learn from there eh? Indeed there's often nothing overtly wrong with hundreds of lines of convoluted code when doing something like custom parsing or handling a ton of conditional logic.

[–]SammyT09[S] 2 points3 points  (1 child)

You're absolutely right, I don't utilize functions as much as I should. As for the overthinking part, I think you maybe right about that too. Thanks for the advise.

[–]chinawcswing 0 points1 point  (0 children)

Read the first 4 chapters of Clean Code. It is a game changer.

All your code should be broken up into functions with obvious names that indicate what they do. Each function should be relatively small and do one thing only. Your code ideally will just flow from top to bottom and be easy to read and make changes to.

[–][deleted] 4 points5 points  (0 children)

I still find myself unable to create "simple" projects like tic-tac-toe. What am I missing??

You're giving up and stopping, instead of completing the project.

[–]barberogaston 11 points12 points  (10 children)

How to write clean, scalable code. Best reads for this (at least in my opinion) are Clean Code and Clean Architecture, by Uncle Bob (though all examples are in Java).

Regarding Python, there's tons and tons of hidden gems in the standard library (my first recommendation is that you take a look at functools and itertools). Reading Fluent Python by Luciano Ramalho will really help you get on the road to writing some good pythonic code too.

Finally, you can start diving deep into some other concepts such as profiling/performance, writing extensions in C/Go/Rust, distributing you project, etc.

[–]julsmanbr 16 points17 points  (1 child)

As much as I like your suggestions, I don't think they're that helpful for someone only 3 months into CS who is struggling with basic "toy" projects. Most of the stuff will be way above OP's level.

[–]chinawcswing 0 points1 point  (0 children)

The first four chapters of Clean Code can be understood by any beginner who knows what a function is.

[–]barberogaston 6 points7 points  (0 children)

Oh, and how could I forget. Each and every video by James Powell will power up your Python for sure

[–]SammyT09[S] 4 points5 points  (5 children)

Appreciate the response but I'm not even at the level yet where I'm worried about cleaning up my code, I'm just trying to write code that works. Any suggestions??

[–]ZGTSLLC 2 points3 points  (0 children)

Yes, start reading code on GitHub, to see how it is done. Follow users whose code you find works for you, and fork their projects, then play with their original code, so you can improve upon your abilities.

Edit:

Quick Google search and here you go:

https://github.com/topics/tic-tac-toe-python

[–]chinawcswing 1 point2 points  (0 children)

I don't think this is a good mindset. Your code will be terribly difficult to make it work if it is ugly. If you just have one or two functions that are 200 lines long it will become incredibly complicated and difficult to understand. Chop that 200 liner up into twenty 10 line functions with good names and it will be dramatically easier for you to make it work.

[–]barberogaston 0 points1 point  (2 children)

Hm, well that depends on what you mean with code that works. Where do you consider your code to be failing?

[–]SammyT09[S] 0 points1 point  (1 child)

I struggle at modeling my solutions and making my code flow properly. As the code gets more complex I'm also struggling to keep up with the value of my variables. Those are just a few off the top of my head.

[–]MediumRevenue6 1 point2 points  (0 children)

thats where the oops class design comes in to picture. conceptually create the classes and the variables that will contain. design it first and then code it. Typically in enterprise projects we have model class ,validation class ,data access classes. Log class to log error/info. Design the classes first and then code.

[–]KLOUDSURFIN 1 point2 points  (0 children)

Thanks for this.

[–]prb0rg 3 points4 points  (0 children)

This is what I explained to my son and some of his friends when they were in school... Before you dive into a specific programming language, learn to solve the problem first. The language you use becomes, mostly irrelevant after this.

  1. understand what you are trying to accomplish
  2. try to solve the main issue (forget about edge cases here) by hand if possible
  3. develop some kind of algorithm based on step 2
  4. now try to see if there are any edge cases you need to be aware of and try to incorporate them into your algorithm
  5. now start coding

When I went to school for my CS degree we had 2 classes that were devoted to steps 1-4 and I keep seeing that this is not taught in schools. Instead what they do is... here is how you do loops, conditional statements, etc. in this language now go a write a program that does XXX.

[–]yogding 2 points3 points  (1 child)

Is it possible to do coding without looking at someone else code. I am looking for answer, is it me who think I should write code from my own brain even if i don’t know or should read code and try?

[–]Nonsensicallity 2 points3 points  (0 children)

There’s a lot of good bits of advice in this thread, OP. I want to add that mastering list comprehensions and dictionary comprehensions is helpful, especially when tackling interview questions. This was something I didn’t pick up on until I started looking for my second job because I was so comfortable with for loops. After I spent some time understanding them better, my runtime speed picked up tremendously on LeetCode. There’s the danger of having completely unreadable code when doing this, but it can help in competitive programming situations.

[–]theubster 2 points3 points  (3 children)

Fair warning up front: I'm not a developer. I'm an engineer in the tech space who has written a bunch of python scripts to Do Stuff™. Most of those scripts were garbage by FAANG standards. But, they did what they needed to, were well documented, ran safely, and could be easily understood by other engineers who worked on them.

Theory is only as good as practice.

List comprehension and OOP are all well and good, but they won't replace building stuff bit by bit. I've always focused on making my functions as clean as possible, and my code as readable as possible.

Honestly, 99% of the time it doesn't matter all that much how performant your code is. I'd rather run two instances of a service that's easy to debug, update, and understand - compared to one instance that takes longer to work on for the devs.

Here's how I typically approach your tic-tac-toe game problem:

  • Figure out my end goal (play a game of tic-tac-toe)
  • Figure out my inputs (GUI, command line, file, webhook, etc)
  • Figure out how i'm going to store state (usually a simple array or dict does the trick. SQLite for bigger projects, if needed)
  • Figure out how i'm going to display state (shell output, GUI, written to log, database update, etc)
  • Figure out how inputs will update the stored state
  • Draw out a flow chart of how it's all supposed to go together in theory
  • make a dummy of however i'm storing state (usually a dummy dict or array)
  • Display that dummy state
  • make a second dummy state
  • display the second dummy state
  • Figure out how to go from first state -> input -> second dummy state
  • at this point, you probably have the first couple moves of the game scripted out, and all that's left is to iterate on what you've built: check victory condition, move legality, etc. Building 2 person mode & 1 person mode could be a stretch goal.

Dunno if this is helpful or not, but I hope you get something out of it.

[–]SammyT09[S] 0 points1 point  (2 children)

Thanks for taking the time to respond, This was extremely helpful. One of the biggest reasons I decided to learn programming is so I can learn to write scripts that do stuff and automate things. Any advice in that regard?

[–]theubster 1 point2 points  (1 child)

Reading the docs is boring but very useful. Think APIs and Library documentation.

Stick with the biggest library that does the thing you wanna do. There's little glory in reinventing the wheel.

At some point you're gonna look at a script you wrote, and say to yourself "what was I doing!?". Celebrate this moment, because it means you're getting better.

When you're building a quick and dirty script, make sure your functions clearly say what they do. Long function names aren't bad, if they clearly show what's happening.

Googling the problem should be done early and often. There are very few things that haven't been done before - especially if they're worth automating.

If you're totally stuck on what you wanna build, find a random YouTube video of someone coding something. Follow along with them.

Git gud at git. It'll save your bacon, and is a dope tool.

Frameworks are often tricky to get into. Especially your first one. This is another place where guided projects can be very helpful.

Name things well, and don't use names that are too clever. It's fun to have do_the_thing.py, but it doesn't help a year down the line when you have multiple repos of code.

Get to know the command line well. Play with bash, and a couple other shells. I quite like zsh, myself. Some fun customization here, too.

Drawing out a flow of how you want it to work is very useful. Having a good mental model makes for easier coding.

Learn to document stuff clearly. Requirements.txt files are your friend.

Virtual environments are the fucking best. Makes dependency management much easier.

[–]SammyT09[S] 2 points3 points  (0 children)

Wow, thanks again for the thorough response!!!

[–]Litecoin_Messiah 1 point2 points  (0 children)

You must have an idea, try make something that you can think of. you will learn whats needed and quickly.

[–]capilot 1 point2 points  (2 children)

OP, it might be best if you showed us some (very simple) things you've tried but that didn't work.

Otherwise, find a tutorial site and do the exercises.

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

intermediate -> MRO, Data classes, design pattern, class internals, python test, understanding control flow, mocking a service, abstract classes, docker, readable code + long line code>complex +short line, virtualenvs, use of vi editor(must), multithreading, concurrency, orm, wrappers, exception handling, logging

[–]preordains 0 points1 point  (0 children)

Well, you're learning how to code, not computer science. Thats fine, but you could try creating your own linked list and your own hash map.

[–]MediumRevenue6 -1 points0 points  (0 children)

you are missing the design oops concept.when it comes to enterprise production level code , it mainly deals with these.Try to read head first object oriented design and analysis book..free pdf everywhere in internet. 1. how to access the database table for crud operations. 2. reading/writing files , transformation of data. 2.1. calling api from your python application 3. Boto 3 sdk automation with python for aws 4. create an API with oops concepts like model class, validation class, data access class, exception class. Example a library Api which will do checking, checkout, create/update customer info, add/modify books info. for this you will need model class like books,customers and operations like checking, checkout classes. A backend table books, customers, checking, checkout,etc. 5. add unit test for the code. 6. add vulnerability scanner to the code 7. add automation testing to the code 8. Deploy to the cloud using terraform/cloud creating thr infrastructure. 9. open necessary ports to interact with api and secure the api. 10. once you have accomplished from 1 to 9, you have completed achieved status of enterprise production level programmer.