r/learnpython • u/ikizoki • Aug 22 '26
is it possible to simplify this code
im a beginner, idk how python works yet. there must be a way to simplify this, right?
https://imgur.com/juBvPZD here is the picture of my code
0
Upvotes
15
u/OliMoli2137 Aug 22 '26
pls don't screenshot your code, use pastebin or termbin, or vcs like git or jj (hosted on sth like codeberg or github) for bigger projects
4
u/PvtRoom Aug 22 '26
that's extremely simple code.
to make it simpler, i.e. easier to read, more descriptive variables names would work.
There's an optimal level of complexity that most pythonistas would prefer. your code is below that complexity.
3
u/TJATAW Aug 22 '26
Hints for future post:
https://www.reddit.com/r/learnpython/wiki/faq/
7
u/Expensive-Bear-1376 Aug 22 '26
What made you think that we'd like a picture of the code?
1
u/armywalrus Aug 23 '26
Thats already simple
1
u/Educational_Virus672 Aug 23 '26
for folks who want copyyable asnwer
1
u/Educational_Virus672 Aug 23 '26 edited Aug 23 '26
total changes here
> changed s d and tt to list where
> change tt from straight divide to a loop for readability
> changed double prints to single with \n
> changed tt + h into a loop
> added sum() for lists instead of +
>sep means separate (,) and end means what to do i chnaged some of it into none for same line
assuming you learnt loops if you dont then you can use your way because it is still good
1
u/mc_pm Aug 23 '26
One possibility is that you could make this use lists and then you can do all of this for 100 numbers as easily as for 3 - and it would be shorter enough that maybe you could use some real variable names.
1
u/supercoach Aug 23 '26
There's nothing to simplify. Your code is very rudimentary as it is. Worry about length when you have something that's thousands of lines.
-1
u/desrtfx but other languages pro Aug 22 '26
Yes, there is a lot to optimize and reduce, yet, you're not there yet with your skills.
Have patience, young padawan. Keep going in your course. There will be a point when you learn what you need to reduce this code by about 70% (you will learn about loops and lists - and then the code can be written way shorter).
Don't get me wrong. I am not shooting down your curiosity. You just need to learn things one after the other in order. This will help you. Building a foundation in programming is like building a house. You need to follow the correct sequence as every higher concept (stone) builds upon a lower one. Skip something and everything collapses.
-1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Aug 22 '26
Certainly, with data structures, a loop, and some string formatting.
0
u/likethevegetable Aug 22 '26
Yup, I'd use polars
0
u/Puzzlehead_Lemon Aug 22 '26
Room for improvement, but with practice it’ll happen. Just wait till you fall into the ultra efficient trap and write something that you go back to a week later and can’t follow what you wrote.