D
D
Dmitry2018-10-25 21:27:42
Python
Dmitry, 2018-10-25 21:27:42

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

Question: what jambs are there / what is not true, what can be simplified?
Immediately, I note that I have not yet reached the functions.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Nekrasov, 2018-10-25
@provocatorr

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")

PS I just don't know how difficult this code is to understand.

J
Juhani Lahtinen, 2018-10-26
@nukler

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 question

Ask a Question

731 491 924 answers to any question