r/pythontips • • Mar 17 '22

Module New to Python. Looking for legibility tips.

Hey guys! I’m following a course right now and some of my friends are saying it isn’t exactly how you would “write code”. The lessons are based more so on problem solving, which I enjoy tbh.

To the chase, I made a Rock, Paper, Scissor game and this was my code:

import random

cinput = random.randint (0, 2)

question = input("What do you choose? Type 0 for Rock, 1 for Paper, or 2 for Scissors\n")

if int (question) == 0: print (rock)

print("The Computer Chose:")

if cinput==1: print (paper) print ("You Lose") elif cinput == 2: print (scissors) print("You win!") else: print (rock) print ("Darn, it's a tie! Try again.")

I repeated this for all 3 outcomes and looked back at my “professor’s” code and it was a lot more simplified.

Is it common to overkill code in the beginning? And how did you guys learn to code simpler? Any tips would be appreciated. Please be kind, I’m still enthusiastic about learning 😅

26 Upvotes

24 comments sorted by

20

u/ElMaledictum Mar 17 '22

As a beginner in any language, the best way to learn is the brute force way as long as you get the logic behind your code.

YOu will learn to shorten your codes as you progress and learn new syntax.

Just take your time.

7

u/TrainrRed Mar 17 '22

These comments are super dope, I really appreciate it guys. You have no idea

8

u/_RKKC_ Mar 17 '22

Does your code work as intended? Yes? Then it works correctly.

That being said, are there ways to make your code better? sure. Don't discount that. Just because it works, doesn't mean that it's optimized. I would suggest taking what you wrote, looking back at the way the course did it and try to determine why they did it that way. There may be a good reason. The curriculum will also likely expose you to a number of fairly basic concepts that will help you in future lessons.

Keep playing and revising. You'll only learn more and more.

1

u/TrainrRed Mar 17 '22

Thanks a ton man, I’ll for sure. Usually I always look back at it. Today was the first time they didn’t do anything funky so their code looked very clean.

3

u/csprance Mar 17 '22

It's common to overkill code even as a professional ;) You learn lots of little language tricks with syntax to make it even more succinct and just better about how to control flow and make things more elegant the longer you code.

Just start poorly making a bunch of projects and looking at open source libs and code on GitHub and before long you'll be picking up all the same little tricks.

It's like art, you have to steal a bunch of other people's style before you develop your own style. Just keep at it!

2

u/TrainrRed Mar 17 '22

Can def relate to the art thing haha. Appreciate it. You all are so nice compared to the art community tbh 😅

3

u/csprance Mar 17 '22

Then you just haven't met enough programmers yet lol jk.

2

u/TrainrRed Mar 17 '22

Ive heard some horror stories. Hopefully it stays amicable 😮‍💨😅

1

u/csprance Mar 18 '22

Nah I'm just joshing you.

Most programmers are amazing people, it's just sometimes the higher level an engineer becomes the more Gods gift to man they think they are.

4

u/Thijmenn Mar 17 '22

Hi there! Awesome seeing somebody start learning Python :-)

Adding to the previous comments, I would suggest you look into PEP. This is Python’s style guide, and adjusting your code so it adheres to PEP will make it look more neat and proficient.

2

u/csprance Mar 17 '22

Even better just get black code formatter and never think about code style again

2

u/Thijmenn Mar 17 '22

I would say that when someone is just starting out it is better to work without an automatic formatter. From my experience, people who start learning a language with an automatic formatter frequently become a "lazy programmer" that is not actually familiar with the theory of why certain styles/constructs are prefered.

Instead, I would recommend OP to use a linter (i.e. code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs) that points out the mistakes, but does not automatically fix them.

u/TrainrRed PyCharm Community has a built-in PEP linter and is just a great Python IDE overall.

2

u/TrainrRed Mar 17 '22

Im using Replit for my courses. Before I run my code, it shows syntax errors and anything else that’s critical to the code failing. It doesn’t correct it, nor does it tell me how to fix it. Just flags :)

2

u/TrainrRed Mar 17 '22

I will be using Pycharm after I get through a few sessions. It’s noted in my curriculum :)

3

u/Thijmenn Mar 17 '22

Awesome, good luck! You can always PM me if you have questions, love to help.

2

u/TrainrRed Mar 17 '22

Appreciate it homie :)

1

u/csprance Mar 18 '22

I disagree. I think code formatting should be automatic and uniform. The bigger the project and the more people that work on it, the more automatic and uniform code styles become necessary. If you're worrying about the formatting of the code you aren't worrying about the flow of the code.

Linters still have their place and you should ideally be using both. A formatter to automatically make your code look prettier and a linter to help you with easily identifiable issues beyond formatting.

You only have so much metal bandwidth to expend each day and worrying about formatting takes that away from real problems.

Let the computer do the work it's good at and you focus on things the computer can't do.

2

u/Thijmenn Mar 18 '22

Oh yes absolutely! Perhaps it wasn’t entirely clear from my previous message, but what I said in my previous message was only applicable to someone that’s just starting out. As you progress, surely use an automatic formatter. Saves you time and headaches ;-)

1

u/csprance Mar 18 '22

Ya, I just think the sooner and faster you start learning and using the tools of the trade the better. I just was mostly disagreeing with the "makes a lazy programmer bit"

2

u/setwindowtext Mar 17 '22

Your solution is fine, and I would have done exactly the same if I was learning programming. It may not be the shortest or the smartest, but it’s very easy to read and understand, and in the real life this does matter. Don’t try to shorten it and impress your professor whatever it takes, instead make sure that it’s readable for someone who speaks English.

1

u/TrainrRed Mar 18 '22

This response makes me feel a lot better. Thanks a ton dude

3

u/Silent_Moose_5691 Mar 17 '22

yes, it’s normal dw. basically you either keep trying to optimize your code after its written or just if a solution seems inefficient don’t let yourself settle on the bad solution and try thinking of another one

good luck

3

u/DivineHermit Mar 17 '22

Sure, when first starting out you'll write a function that's a paragraph long, for example, but it works. As you learn more & better ways of doing things that will get refined to half a paragraph and then to the realization it could be three small function that each do a specific part and is shorter and faster.

It's a learning curve, nobody starts able to code the most efficient software straight away, you and your code will improve with practice and experience.

"Make it work, make it right, make it fast." Kent Brek (I think)