Reputation: 23
I have been having trouble with Python in that I can't stop scientific notation and convert to a floating number. I have tried multiple methods, and none of them have done what I have wanted. Below is the code for a random number generator that continues on until it finds the number 7. At the end, it gives you statistics on the run. One gets added to x every time the loop goes through Thank you in advance. Edit: The output is usually something like this:
Your computer just went through 279754 integers to find 7!
Your computer just took 4.462186096363586e-07 seconds to run!
Your computer took 1.5950392474686997e-12 seconds per integer!
What I have is:
x = 0
c = time.clock()
print("Your computer just went through " + str(x) + " integers to find 7!")
print("Your computer just took " + str(c) + " seconds to run!")
print("Your computer took " + str(c / x) + " seconds per integer!")
Upvotes: 2
Views: 896
Reputation: 3106
Use .format()
instead. To not get the scientific notation (the e-number
part), enter the specified length of the number.
import time
x = 210513
c = time.clock()
print("Your computer just went through {} integers to find 7!".format(x))
print("Your computer just took {0:.20f} seconds to run!".format(c))
print("Your computer took {0:.20f} seconds per integer!".format(c / x))
The .20
part means that the float will be 20 numbers after the decimal point. Change the number to change the length, such as .50
will have the float have 50 numbers after the decimal point and so on.
Computers run rather fast so the time can be surprisingly low at times.
Upvotes: 0
Reputation: 728
Use format and specify length after floating point.
print("Your computer just went through {} integers to find 7!".format(x))
print("Your computer just took {} seconds to run!".format(c))
print("Your computer took {0:.8f} seconds per integer!".format(c / x))
Your computer just went through 371 integers to find 7!
Your computer just took 0.026945 seconds to run!
Your computer took 0.00007263 seconds per integer!
Upvotes: 1