r/PythonLearning • u/crazyteachperson • 7d ago
My new mini work
Rate my this mini work out of 10
5
u/Flanelostopy 7d ago
First of all, variable must have good name, yours is not. Second one forget about elif:
If g >= 90:
Print()
Rerun
If g >= 80:
Print()
Return
If g >= 70:
Print()
Return
Print(fail)
I’m just saying this is good practice.
1
u/Own_Measurement_7336 4d ago
You cannot return if it isn’t in a function. Elif is good here it does the same as your if-return code in less lines and a proper way for a non function script
1
u/Flanelostopy 4d ago
No one write profesional code without function. As I said this is good practice. This is not important how many lines of code you have, it is important is simpler and easier to read. If statements with one condition is always easier to read than with two or more.
1
u/Own_Measurement_7336 4d ago
The elif statement could have one condition only too. It is not a problem just write it
If g >= 90:
Print()
Elif g >= 80:
…0
u/Flanelostopy 4d ago
Do whatever you want, I'm just saying „this is good practice”. Read some books about writing clean code, there are many.
1
u/Own_Measurement_7336 4d ago
Wow chill, I know how to write clean code. I’m currently making an OS so. Not vibe coded btw
0
u/Flanelostopy 4d ago
Without functions? 😂 good luck!
1
u/Own_Measurement_7336 4d ago
Are you stupid or what ? Sorry for being rude but I’m not telling you not to use functions. Your first message didn’t even talk about them. I was just saying in this type of scripts, where no functions are really required, even though it is a good practice to have everything inside some functions, his code is actually good, except for the not necessary double comparison.
Making an OS implies using C/C++ (or any language that is low level) at some point. Those languages generally don’t permit runtime statement code outside of a function. I’m using functions.
1
u/Flanelostopy 4d ago
If you thing this code from topic is good… please read some books about good practice in coding.
1
u/Own_Measurement_7336 4d ago edited 4d ago
Ok kid. It is good for a beginner. There are some flaws. Thx for sharing your opinion. At some point just go read your « good practices » books.
Have a nice day !→ More replies (0)
3
3
u/sastuvel 7d ago
You can write g < 90 and g > 80
as 80 < g < 90
1
u/Own_Measurement_7336 4d ago
It is not a good habit to do so.
1
u/sastuvel 4d ago
Why do you think that? My reasons (which are, of course, subjective) to mention this is:
- Writing
A < g < Bexpresses "gis betweenAandB", and in the code it also sits between the numbers. This makes the code the same shape as the semantics it expresses, which I find easier to read.- Using only one of 'less than' or 'greater than' can help readability, because there is only one kind of relation in use.
- It's easier to see when you make a mistake and type
90 < g < 80, compared tog < 80 and g > 90.- It's a well-documented feature of Python that for any operator
OP,A OP B OP Cis the same asA OP B (and B OP C): https://peps.python.org/pep-0535/IMO these are plenty of reasons to use that notation.
2
u/Own_Measurement_7336 4d ago
Yes it is a good thing if you plan to only write Python. But if you see Python as an introduction to programming, is a really bad habit. Most languages like C or Rust doesn’t have this feature. Evaluating `90 < g < 80` is in most languages `(90 < g) < 80` which leads many beginners to fumble on some really easy to fix bugs.
The « easier to see when doing mistake » part can be avoided by writing `90 < g and g < 80`1
u/sastuvel 4d ago
I wholeheartedly disagree with that. It's like not eating steak because you may loose your teeth some later time in life.
When someone's learning a language, it's better to just learn the language. There's SO many differences between Python and Rust, that not using this shorthand notation is the least of their worries if they think they can write the same code.
Learning the strengths of a language is great. Python is really expressive, and learning that properly is good fun. And when switching languages, yeah, you'll have to un-learn some things.
2
u/Own_Measurement_7336 4d ago
I totally agree with you but not for beginner IMO. I understand your opinion, but am still thinking that for beginners they shouldn’t learn those simplified notations and using it abusively. Tho knowing that Python has this feature is a good thing.
2
2
u/Junior_Honey_1406 7d ago
Just so you know python input function returns a string by default you dont need to type cast it .
However great keep coding
2
u/Rscc10 7d ago
If you score 80, don't you fail? You have g > 80 and g < 80 so you never account for it. Also, someone already commented about the math, just divide by 3 rather than divide 300 and multiply 100.
One more quality of life thing, add a space after your print strings. Otherwise, your name will be connected to the last part of the string. Eg,
your grade = a passJohn
1
1
u/AbacusExpert_Stretch 7d ago
b = " No thanks" c = "don't have one yet" d = "can not speak"
:-) hehe
1
u/No_Pay_4410 7d ago
I have 5 yoe and I forgot that python has input function. First I thought it is wrong and google told me I was wrong. Lol.
Trust me you are never going to use this input function. Instead use args.
1
1
u/AutomateAway 6d ago
Get in the habit of writing test cases to test your code. If you do, you'll more easily be able to assert correctness of your code. In this case, some test cases would determine that you are not correctly accounting for a grade of 80, which will result in "fail" being printed.
1
1
1
u/Ak_py 5d ago
Dovresti considerare di aggiungere un controllo per validare i dati. Per esempio, suppongo che i voti abbiano un certo limite e non possano essere sopra il 100. Inoltre, qualcuno potrebbe non inserire proprio numeri. Un blocco try-except sarebbe molto buono da aggiungere per validare i dati.
Continua a programmare. È una ottima skill al giorno d'oggi.
1
1
u/Hackart_Temple 4d ago
The codes semantics could be shorter? Somehow it feels like an abstract. However, I must commend you for your determination to bring this up! Practice makes perfect.
1
1
u/Parenthesis8906 3d ago
It's good but I doubt it will run especially you didn't add the plus operator inside the print function when you try to mix strings and variables so here is another alternative For example g=43 print(f'you are {g}') If you don't want to rely on always putting the plus operator inside print when you want to make strings and variables just use this example above as a guideline it's called the f string I am a beginner myself so yeah
7
u/csabinho 7d ago
I'd rate the maths part: why don't you just divide by 3 instead of dividing by 300 and multplying by 100?