0

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!

IIlIll IIIlII
  • 51
  • 1
  • 6
  • 2
    It's the `%i` that explicitly instructs Python to make an integer representation. Try something like `%f` instead. – Paul Panzer Dec 28 '17 at 03:40
  • or just use `f"Ideal probability: {idealProb}"` (which is new in Python 3.6) – Eevee Dec 28 '17 at 03:44
  • Possible duplicate of [round() in Python doesn't seem to be rounding properly](https://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly) – Carol Chen Dec 28 '17 at 03:48

2 Answers2

1

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

chessprogrammer
  • 768
  • 4
  • 15
0

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.

John Cummings
  • 1,949
  • 3
  • 22
  • 38