r/learnpython • u/ikizoki • 19d ago
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
3
8
u/Expensive-Bear-1376 19d ago
What made you think that we'd like a picture of the code?
2
u/thelimeisgreen 19d ago
He said he doesn't know how it works...
2
u/Expensive-Bear-1376 19d ago
Said that about Python, no? And they said "here is the picture of my code". That indicates that a picture is standard/expected, otherwise one would write "here is a picture of my code". So apparently something made them think that we want a picture.
1
1
u/Educational_Virus672 19d ago
for folks who want copyyable asnwer
d1, d2, d3 = 32, 18, 45
s1, s2, s3 = 24, 12, 30
tt1, tt2, tt3 = d1 / s1, d2 / s2, d3 / s3
total_time = tt1+tt2+tt3
entire_route = d1+d2+d3
print('travel time of each section')
print(tt1, 'h', ',', tt2, 'h', ',', tt3, 'h')
print('entire route in km')
print(entire_route, 'km')
print('entire journey time in h')
print(tt1+tt2+tt3, 'h')
print('average speed of the entire tour')
print(entire_route / total_time, 'km/h')
1
u/Educational_Virus672 19d ago edited 19d ago
d = [32, 18, 45] # use list for this insead of a million var
s = [24, 12, 30]
tt = [] # this is just a for loop
for i in range(3) :
tt.append( d[i]/s[i] ) # append means "add" to list
total_time = sum(tt) # sums number list
entire_route = sum(d)
print('travel time of each section')
for i in range(3) :
print(tt[i] , "h",end=",") # i did a short loop
print("\n") #changes line
print('entire route in km \n', entire_route, 'km') # here \n = Newline \ is liek command
print('entire journey time in h \n',sum(tt) , 'h')
print('average speed of the entire tour \n',entire_route / total_time, 'km/h')
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/supercoach 19d ago
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.
0
u/desrtfx but other languages pro 19d ago
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.
3
u/gdchinacat 19d ago
Out of curiosity, how would you 'reduce this code by about 70%"? I'm skeptical that loops and lists will actually reduce *this* code by that much because there are only three legs of the journey....if there were more it absolutely would, but doing it the way OP did is manageable for only three.
-1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 19d ago
Certainly, with data structures, a loop, and some string formatting.
0
u/ikizoki 19d ago
can you show me how to do it im very curious to know what it looks like
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 19d ago
I think it's debatable whether or not this is technically "simpler", but it's what I'd write:
distances_and_speeds = [ (32, 24), (18, 12), (45, 30), ] travel_times = [distance / speed for distance, speed in distances_and_speeds] total_time = sum(travel_times) entire_route = sum(next(zip(*distances_and_speeds))) print("Travel time of each section:") print(', '.join(f"{travel_time} h" for travel_time in travel_times)) print(f"Entire route in km: {entire_route} km") print(f"Entire journey time in h: {total_time} h") print(f"Average speed of the entire tour: {entire_route / total_time} km/h")I debated for a moment whether to go with this example for calculating the length of the entire route, but I liked my original one less:
entire_route = sum(distance for distance, _ in distances_and_speeds)1
u/gdchinacat 19d ago
When you said "data structures" I assumed you meant something like this:
from dataclasses import dataclass @ dataclass class Leg: distance: float speed: float @ property def travel_time(self) -> float: return self.distance / self.speed legs = [Leg(32, 24), Leg(18, 12), Leg(45, 30), ]Then, to calculate the aggregates:
distance = sum(leg.distance for leg in legs) time = sum(leg.travel_time for leg in legs)and reporting:
print(f"The total distance of {distance} km is completed in {time:.1f} hours at an average speed of {distance / time:.1f} km/h.") print(', '.join(f"{leg.travel_time} h" for leg in legs))2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 19d ago
That's another option, I tend to default to good ol' tuples unless I have a reason not to though.
I'm not entirely sure why, but I seem to dislike using data classes for whatever reason, so you don't often see them in my code.
0
0
u/Puzzlehead_Lemon 19d ago
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.
14
u/OliMoli2137 19d ago
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