I just found out how to make random numbers in Python, but if I print them out, they all have 15 decimal digits. How do I fix that? Here is my code:
import random
import os
greaterThan = float(input("Your number will be greater than: "))
lessThan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))
os.system('cls')
if digits == 15:
print(random.uniform(greaterThan, lessThan))
if digits == 14:
print(random.uniform(greaterThan, lessThan))
if digits == 13:
print(random.uniform(greaterThan, lessThan))
if digits == 12:
print(random.uniform(greaterThan, lessThan))
if digits == 11:
print(random.uniform(greaterThan, lessThan))
if digits == 10:
print(random.uniform(greaterThan, lessThan))
*(this just continues down to 0)
I know you can do like print("%.2" % someVariable)
but the random numbers Python creates with this method are not saved in any variables. At least I think so.
I would also like to know if there is a way of letting a variable choose the amount of decimal points, like print("%." + digits % anotherVariable)
but I tried tha out and of course it failed.
I really got no idea. Hopefully you can help. Thanks.
Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 x 10308.
If you want to take whatever number you get from random.uniform
and truncate it to the specific number of digits you can use the round()
function.
It allows you to round to a specific precision. For example:
import random
greaterThan = float(input("Your number will be greater than: "))
lessThan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))
rounded_number = round(random.uniform(greaterThan, lessThan), digits)
print(rounded_number)
You can also handle float precision using .format()
:
print float("{0:.2f}".format(random.uniform(greaterThan, lessThan)))
Where the 2
can be replaced by how many digits after decimal you want.
I'm not sure what you mean by "fixing it", since what you're talking about is pretty fundamental to how computers perform what's called floating-point arithmetic.
However, that said, I am pretty sure you are looking for the decimal module:
import decimal
Best of luck!
Edit:
Taking a second look at your post, this feels wonky. Is this a homework assignment? Why have the digits
variable at all, if you're not doing anything with its value? Sure, you check it (over and over and over again), but you're doing the same thing each time.
If you're actually doing something differently for each value of digits
, then you should use if-elif-else:
if digits == 15:
do_stuff()
...
elif digits == 14:
do_other_stuff()
...
elif digits == 13:
do_even_moar_different()
...
...
else:
and_now_for_something_completely_different()
But that's ugly, and Python is supposed to be pretty (import this
).
if digits > some_value:
do_stuff()
...
elif digits <= some_other_value:
do_something_else()
...
I do encourage you to read the floating-point arithmetic link, since this is very important to understand, at least a little.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With