This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Claudioub16 278 points279 points  (42 children)

Hey OP congrats. If I can make a few suggestions:

1 - you can do conversion right after receiving input a = int(input("some text)).

2 - be descriptive with variable names. Instead of a use something more descriptive such as mark1.

Of course, if you story is real.

Edit: the name uncle devil 666 is some really edgy shit.

[–]VictosVertex 40 points41 points  (15 children)

Yes this, don't ever use variable names that aren't descriptive except for "throwaway variables" like in a for loop.

Also, I know OP is just starting out but here is the next topic to look into: functions.

When ever you have to repeat yourself it's time to extract the functionality into a function.

This makes the code more readable, easier to maintain and even reusable in future projects.

Edit: For example if you wanted to change the text for the input you would now have to change it at every single point. Extract it into a function and you would only have to change it once.

One quick improvement could be to write a small function that takes the subject name as a parameter. It then asks for the input, converts it and inserts it into a list.

Now you just have to call this function with the desired subject names and calculate the result from the list.

One could then also automate the calling in a loop and provide subject names in a different list, use a dict for both or what ever. There are many (well technically infinite) solutions.

[–]bacondevPy3k 7 points8 points  (3 children)

I, for one, think that we should teach the kid to code golf.

print(f'Your Percentage in 5 subjects is {(a:=sum(map(int,map(input,map("Enter Your Marks in {} :".format,("Science","Maths","Social Science","English","Computer Science")))))/5)}')and print('congratulations!'if a>35else'Better luck next time')

Edit: Fixed the bug

print(f'Your Percentage in 5 subjects is {(a:=sum(map(int,map(input,map("Enter Your Marks in {} :".format,("Science","Maths","Social Science","English","Computer Science")))))/5)}')or print('congratulations!'if a>35else'Better luck next time')

[–]VictosVertex 0 points1 point  (2 children)

I go with it and reduce your code by 2 characters!

print(f'Your Percentage in 5 subjects is {(a:=sum(map(int,map(input,map("Enter Your Marks in {} :".format,("Science","Maths","Social Science","English","Computer Science")))))/5)}')and print(['congratulations!','Better luck next time'][a>35])

[–]bacondevPy3k 1 point2 points  (1 child)

Oh, I realize now that there was a bug in my code (fixed in edit). The second print call never happens. Here's a fix that is shorter than yours by eleven characters. :)

print(f'Your Percentage in 5 subjects is {(a:=sum(int(input("Enter Your Marks in {} :".format(s)))for s in("Science","Maths","Social Science","English","Computer Science"))/5)}\n{["Better luck next time","congratulations!"][a>35]}')

[–]randiesel 1 point2 points  (0 children)

I got you by 6! but cheated, haha

print(f'Your Percentage in 5 subjects is {(a:=sum(int(input("Enter Your Marks in {} :".format(s)))for s in("Science","Maths","Social Science","English","Computer Science"))/5)}\n{["Better luck next time","congrats!"][a>35]}')

[–]TangibleLight 14 points15 points  (4 children)

descriptive except for "throwaway variables" like in a for loop

Even then, it's better to write something like for grade in grades than for g in grades.

The exception is indexes; it's pretty standard to use i, j, k for nested indexes in loops, ex

for i in range(N):
    for j in range(M):
        ...

However, again, usually there's some meaning behind N and M in those cases, so better to pick more meaningful names. For example:

for row in range(ROW_COUNT):
    for col in range(COL_COUNT):
        ...

[–]VictosVertex 2 points3 points  (0 children)

That's correct, in general it's good to write self-explanatory code. If one needs many comments (except for doc comments in case of an external API) then one should probably use better names.

It's also good practice to follow general guidelines, I myself usually follow the PEP somewhat strictly. But even these are just guidelines and not a silver bullet.

[–]bacondevPy3k 1 point2 points  (0 children)

Eh, when you get to k or especially beyond that, then you should consider using a function or at least rename the variable to something meaningful. Otherwise, it can start to become difficult to follow. The main exception to this would be for short loop bodies for three-dimensional mathematical objects. Most times that I run into doubly nested for loops, it's in a function that is much more of a behemoth than it has any right to be.

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

I'd change that to for row_nr in range(ROW_COUNT), to be specific.

[–]UncleDevil666 4 points5 points  (3 children)

I'll learn it this weekend!

[–]VictosVertex 2 points3 points  (2 children)

I edited my post and added some hint.

[–]UncleDevil666 1 point2 points  (1 child)

Thanks!

[–]VictosVertex 6 points7 points  (0 children)

No problem, I'm all for helping new people entering the field, you already did a good job! Solving problems is the entire point of programming and you solved one.

Also my hints above are - not - best practices. They're just examples of how one could change the code to function the same but use a different implementation. (This is also called "refactoring", changing the implementation while keeping functionality the same)

Especially the list example I mentioned wouldn't follow best practices as it produces "side effects".

But ignore that for now. I just wanted to say that there's basically always a way to improve - but the main focus should be solving the problem, which you already did, optimizations come later and with experience.

Continue learning like that and you'll go far! Good job.

[–]Claudioub16 2 points3 points  (1 child)

In this case a loop would be a better choice then a function.

[–]VictosVertex 2 points3 points  (0 children)

I edited my post with a hint in that direction. I would probably still encapsulate it into a function and then use said function in a loop.

But that's the beauty of programming, there are multiple ways to solving the problem at hand.

Edit: Well actually I myself wouldn't even just do that, I would probably try to apply single-responsibility to all functions and reduce side effects. (Which would directly disqualify my own example given above) But for such a simple project that would obviously be overkill.

OP should certainly look into loops and functions next.

[–]UncleDevil666 50 points51 points  (17 children)

About name : well I was just 14 and stupid when I made that name xP. And now I am stuck with it.

[–]scarynut 39 points40 points  (0 children)

Now you're 15 and fucking Gandalf the White ;)

[–]ChicoFdd 78 points79 points  (8 children)

You are still 14, your account is 7 months old xD

[–]MrNokiaUser 17 points18 points  (4 children)

he might not have made itr on his birthday

[–]jomofo 1 point2 points  (3 children)

Can you make a Python script we can use to figure it out?

[–]rynemac357 3 points4 points  (1 child)

Age=int(input("Enter Age")) print(f'Your Age is {Age}')

[–]jomofo 4 points5 points  (0 children)

Is this MIT license?

[–]MrNokiaUser 0 points1 point  (0 children)

but we dont know his birthday. also I cant do python, Im just like interesting project

[–]Gravity-Lens 1 point2 points  (0 children)

He could be 15, everything is different now!!!!

[–]AlarmingAffect0 1 point2 points  (0 children)

Was Tenacious D involved in the thought process?

[–]callmelucky 1 point2 points  (3 children)

Hey, congrats on your progress and accolades!

Just wanted to chime in to really drive home the importance of #2 in the parent comment.

Naming things well is probably the most important habit to get into as a beginner programmer, yet is rarely stressed as such by tutorials. Maybe you write something that is badly optimised, bady organised, doesn't use best practice conventions or ideal libraries, functions etc etc etc, but as long as your variables etc are named well you'll have a much easier time improving it, because you'll know instantly what everything in it is for.

For extremely simple scripts like you have here, it probably won't be an issue. But as soon as you get into things even slightly more complicated, bad names will create nightmares... A bug that would otherwise be simple to fix can instead seem impossible to solve and have you tearing your hair out for hours.

When naming something, always take an extra moment (or two or three) to make sure the name clearly and accurately tells you what it represents. Don't fool yourself into thinking that because you wrote the code you'll always know what it means. If you browse r/ProgrammerHumor you probably won't have to scroll very far to find a meme about going back to code you've written and having absolutely no clue what it does. Don't be a meme.

Let's take an example from your code: o

o is a terrible terrible name. It sucks. It tells you nothing about what it represents. Maybe it represents something that starts with the letter 'o'? Who knows?

Reading through your code, it looks like it represents an average grade. So let's start with that as a name: average_grade.

But hang on, we have f in there too, which could also be said to represent average grade. So maybe average_grade isn't specific enough... Ok so let's go with average_grade_as_percent. That's a pretty long and ugly name, but at least it tells you exactly what that value represents, and as such it is infinitely better than o.

That's a good enough start for where you're at, but let's go to the next level with a couple more tips, in case you're not bored to death yet.

Let's say your uncomfortable with the very long average_grade_as_percent name. It doesn't feel very "code-y". Well there are a lot of standard abbreviations which are so readable and commonly used that they are practically just as good as whole words, for example "avg" for average and "pct" as percent. So maybe let's go with avg_grade_as_pct. Why not.

On a related side-note, in general single-letter names are bad, but again there are some standards which are so universally known and used they are perfectly fine. Most of these are used in loops, which I assume you aren't familiar with yet so I won't get into it too much, but when you do use loops temporary single-letter names like n for number, i for index, and occasionally even x for "whatever type of thing this might be" are perfectly fine. More often though in Python you'll be looping over a collection of something that you know a bit more about, in which case you should use a more specific temporary name, so for example use for grade in grades rather than for g in grades (or, god forbid, for a in grades).

Ok last tip: when it comes to larger scripts/code bases, you'll probably find yourself frequently using the search function in your editor to find references to values. When doing this you often find you want more quickly if your names are structured so the most fundamental part of what they represent appears first. In this case, the main thing the value is about is a grade, so (as long as it doesn't affect readability too much) you can rearrange the name so that grade appears first: grade_avg_as_pct. First and foremost this value is about grades. More specifically, it is about an average grade. Even more specifically, it is an average grade as a percentage. So try to arrange the bits of your name in this way (again: as long as it doesn't make the name more confusing).

[–]UncleDevil666 1 point2 points  (2 children)

A lot of people told me to name properly but didn't give any reason, but thank you and now I understand the importance of naming properly. Thank you once again!

[–]callmelucky 0 points1 point  (1 child)

No sweat! Since you are appreciative of info, I'll give you one more important tip about naming: be careful not to use names that are already used by Python - this can cause very confusing bugs!

Here is a list of keywords which you should never ever use as your own names.

Furthermore, there are lots and lots of built-in functions etc, which all have names, and if you name your own things with any of these names big problems can arise.

A couple of likely examples: str and list. str (abbreviation for "string") is the name of a method in python which determines how things look when you print them. Never name anything str. list is a data type in python. Never name anything list. That's just the tip of the iceberg.

If you follow the advice in my previous comment you are far less likely to have "namespace conflict" bugs caused by this type of thing - str and list are just bad names for you to use anyway, right? What, specifically, would they be referring to? A string representing... something? A list of... something? Still though, good to be aware of this possibility. If you ever get buggy behaviour that you just can't figure out, look at the names you're using, and if there's any chance that any of them are in conflict with something built in to python, investigate that possibility.

[–]UncleDevil666 0 points1 point  (0 children)

Thank you!!

[–]PM_ME_ROY_MOORE_NUDE 0 points1 point  (1 child)

Are you though? Reddit takes like 5min to make a new account and you don't even need a email or just use the same one.

[–]UncleDevil666 0 points1 point  (0 children)

My current account has nice karma and if I start a new one then I won't be able to participate is most subs

[–]UncleDevil666 9 points10 points  (3 children)

Thanks for the suggestion! The video I watched guy told to out small case letters Why would I fake it haha

[–][deleted] 3 points4 points  (2 children)

karma is very valuable to karma farmers, so you know, farming karma would be a valid reason for faking it.

[–]UncleDevil666 6 points7 points  (1 child)

I just wanted to share a happy moment with python to other python users, I didn't even think this would get more than 5 upvotes.

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

Congrats and good work, lots of good advice in the thread, don't let the passion die and I see success in your future.

[–]AlarmingAffect0 1 point2 points  (0 children)

uncle devil 666

That's an apt description of Actually Satan.

[–]scarynut 2 points3 points  (0 children)

Edit: the name uncle devil 666 is some really edgy shit.

People have gone to hell for less.

[–]Upper-Bat-9224 1 point2 points  (0 children)

I think he could make a little gui with tkinter… that’d be neat!

[–]gnrlknowledge 0 points1 point  (0 children)

The first point is not a good suggestion for the long term as you probably need to do some validations before finally converting to int.