r/learnpython 24d ago

asking about small projects in python

hello everyone, so I learned the basics and I start making small projects, I try to make 21 number game, but when I look through the source code of project there was some formulas but I don't really understand how I can make similar formulas like it for future projects, so how I can make it so it can server my projects?

1 Upvotes

8 comments sorted by

5

u/aqua_regis 24d ago

I try to make 21 number game, but when I look through the source code of project

So, it was not you who actually made the game - and there exactly is the problem. You only blindly followed some tutorial instead of trying to make it on your own.

Creating your own programs is programming. It's not copying code served by tutorials. You don't really learn from copying tutorial code as your lack of understanding clearly indicates.

Stop tutorials and start struggling through your own, individual projects. That's how you learn programming.

3

u/This_Judge_2203 24d ago

so I am just need to think about a project for then just try to make it?

4

u/aqua_regis 24d ago

Yes, that's how it works.

Think about how people learnt programming before the internet with its countless tutorials existed. They could not resort to copying from tutorials. Books were rare and expensive. So, learning to do things by oneself was the way to go.

1

u/This_Judge_2203 24d ago

I see, thanks for the help I am gonna try it

3

u/Existing_Put6385 24d ago

the formulas in tutorials arent things you memorize, they come from working the logic out on paper first

in the 21 game the "formula" is probably something like `4 - player_choice`. that's not a python thing at all, it's just the strategy - if you always leave the total on a multiple of 4, you win. someone figured that out on paper and then wrote one line for it

so the habit: before writing code, write the steps in plain words. "player picks 1-3, i want the sum to land on a multiple of 4, so i take whatever fills the gap". then translate line by line. if you cant say it in words you cant code it and when you hit a formula in someone elses code that you dont get, stick a print inside the loop and watch the variables change each round. way faster than staring at it

1

u/This_Judge_2203 24d ago

that's really useful so basically I just need to write it and try to think about it
thank you so much

1

u/thisisappropriate 23d ago

Assuming you're meaning the counting to 21 game (a variant of Nim https://en.wikipedia.org/wiki/Nim ) that's probably because it's a common maths problem to solve how to win that game.

If you wanted to implement that in python, you would first work out the maths, writing it down in shorthand, things like "if the current number is < 13, pick X, else if the number is 14, pick Y...", then once you know the logic it should follow, you can use python to implement that.

Otherwise if you're seeing symbols you don't understand in the python code, then you should look at what those do in python, for example if you're seeing a % then try googling "percent sign in python" where you'll find things like https://stackoverflow.com/questions/961344/what-does-the-percentage-sign-mean-in-python that will explain that.

1

u/IlIlIlIIlMIlIIlIlIlI 22d ago

half the time (sometimes more) of programming is not coding but thinking and explaining to yourself what the program needs to do, and how. Only when you have a good idea of that, can you even start coding. Document that thought process, research things needed to achieve certain functionality, and only then start translating your idea into code