-2

Below is my sum program in Python.

import time
start_time = time.time()

sum = 0
for num in range(0,10000001):
sum = sum + num
print(sum)
print("--- %s seconds ---" %.2f (time.time() - start_time))

Output:

50000005000000
--- 6.02535605430603 seconds ---

The question is how to get the output up to 2 decimal points.

Prune
  • 76,765
  • 14
  • 60
  • 81
parachamk
  • 23
  • 4
  • Instead of `%s` you need to use `%.2f`. Apparently you tried to do that but got confused. Simply change the last line to `print("--- %.2f seconds ---" % (time.time() - start_time))` – mkrieger1 May 06 '20 at 23:08

1 Answers1

1

just stick with Python 3.x formatting and along with that, you have more predefined functions to make your life easier, you have more formatting options though.

import time
start_time = time.time()

sum = 0
for num in range(0,10000001):
    sum = sum + num
print(sum)
print("--- {} seconds ---".format(round(time.time() - start_time,2)))