Answer the question
In order to leave comments, you need to log in
How can one shorten/optimize the solution to a problem in Python?
There is a task:
If you were now on the moon, your weight would be 16.5 percent of the earth. If you put on one kilogram of weight each year for the next 15 years, what would your lunar weight be for each of your annual visits to the moon until year 15? Write a program that prints your lunar weight each year using a for loop.
My decision:
earth_weight = 70
moon_index = 0.165
moon_weight_2018 = earth_weight * moon_index
print('Ваш вес в 2018 году: ' + str(moon_weight_2018))
for i in range(1, 16):
print(i, round(earth_weight * moon_index, 2))
earth_weight += 1
Answer the question
In order to leave comments, you need to log in
Something like this:
weight, moon = 70, 0.165
print("Ваш вес в 2018 году:", weight * moon)
print(*[str(i) + " " + str(round((weight + i-1) * moon, 2)) for i in range(1, 16)], sep="\n")
If slightly optimized then
earth_weight = 70
moon_index = 0.165
year_now = datetime.date.today().year
for i in range(1, 16):
print(f"Ваш вес в {year_now} году: {round(earth_weight * moon_index, 2)} кг" )
earth_weight += 1
year_now +=1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question