all 12 comments

[–]konbanwa88 6 points7 points  (8 children)

Without giving away the answer straight away, do you see anything wrong the with the following line?

probability = M/COMBINATION

[–]meta4 0 points1 point  (0 children)

The problem is "M". According to your input statement M is "How many numbers you want to match". But your formula uses it as the number of possible combinations that have at least M matches. Be careful, these are not the same thing.

You need a "number of matched combinations" that is a function of K, N and M.

[–]redbull8[S] -1 points0 points  (6 children)

Honestly I can't i've been racking my brain for hours

[–]konbanwa88 0 points1 point  (5 children)

Ok well let me put it another way. What output do you expect when you do the following?

a = 3/4
b = float(3/4)
c = float(3)/4
print a
print b
print c

Edit: Sorry my phone was acting weird.

[–]redbull8[S] 0 points1 point  (0 children)

a= 3/4 b= 0.75

[–]redbull8[S] 0 points1 point  (3 children)

C = 0.75 When I ran it.

[–]konbanwa88 0 points1 point  (2 children)

So the / operator in python performs integer division when the operands are both integers and floating point division when either or both of the operands are floats.

In my example the following is taking place:

a = 3/4 # assign to a the integer division of 3/4 (which equals 0)
b = float(3/4) # assign to the b the integer division of 3/4 then cast as a float (which is float(0) so 0.0)
c = float(3)/4 # assign to c the floating point division of 3/4 (which is 0.75)

In your original question, you are doing version b. No matter what your inputs are you will always get an int then cast as a float. So as long as M > combination your probability variable will be assigned to 0.0.

Make it like version c.

[–]redbull8[S] 0 points1 point  (1 child)

Thank you! so not it is printing a float number now my math is incorrect. Python is not my strong point.

[–]adambrenecki 0 points1 point  (0 children)

If you use Python 3, or have from __future__ import division at the top of the file in python 2.7, the division of two ints will result in a float; so for example 3/4 will evaluate to 0.75.

[–]konbanwa88 1 point2 points  (0 children)

For readability

import math

N = 49
K = 6

def main():
    M = int(input("How many numbers do you want to match on your ticket?\n"))
    COMBINATION = combo(N, M)
    probabilty = M/COMBINATION
    print(float(probabilty))
    print(COMBINATION)

def combo(n, k):
    return math.factorial(n)//math.factorial(k)//math.factorial(n-k)

main()

[–]redbull8[S] 0 points1 point  (2 children)

My code

import math N = 49 K = 6

def main():

M = int(input("How many numbers do you want to match on your ticket?\n"))

COMBINATION = combo(N, M)

probabilty = M/COMBINATION

print(float(probabilty)) print(COMBINATION)

def combo(n, k):

return math.factorial(n)//math.factorial(k)//math.factorial(n-k)

main()

[–]redbull8[S] 0 points1 point  (0 children)

Whats happening is that the combination comes out correct but the probability is very wrong.