Reputation: 51
I'm new to python so sorry if I word my question improperly but bear with me. So basically I am making a program to calculate the odds of randomly generating the chosen sides of a (x) sided dice. Problem is when I try to print the variable "idealProb", it seems to rounds down to 0 unless its 100% probability, however in the debugger it says the actual decimal it is only when it prints into the terminal that is gets messed up.
import random
import sys
import os
import time
clear = lambda: os.system('cls')
clear()
rolls = []
sides = []
x = 1
totalSides = int(input("How many sides do you want this die to have?"))
clear()
numSides = int(input("How many of those sides are you trying to land on?"))
nS = numSides
while numSides >= 1:
clear()
s = int(input("Possibility %i:" %(x)))
sides.append(s)
numSides -= 1
x += 1
idealProb = nS / totalSides
print("Ideal probability: %i" %(idealProb))
P.S. Any other comments on my code, any advice on how to make it less messy or a easier way to do what I'm doing please let me know I am fairly new to Python and coding in general, as Python is my first language. Thank you so much for all the help!
Upvotes: 0
Views: 602
Reputation: 2184
Change your last line to output idealProb
as a floating point number by using %f
instead of as an integer with %i
as you have it. That would make the last line:
print("Ideal probability: %f" %(idealProb))
See the string format documentation for more details.
Upvotes: 0
Reputation: 788
When you write %i I'm the print command you explicitly tell Python to truncate the value to an integer. Read this http://docs.python.org/2/library/stdtypes.html#string-formatting-operations
Upvotes: 1