LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

One ticket probability

Screen Link:

My Code:

Defining factorial function

def factorial(n):
    factorial = 1
    if int(n) >= 1:
        for i in range (1,int(n)+1):
            factorial *= i
    print("Factorial of ", n, " is:", factorial)
    # Writing a combination function in Python
def combinations(n, k):
    numerator = factorial(n)
    denominator = factorial(k) * factorial(n-k)
    return numerator/denominator
# Writing a function for calculating the probability of winning a big prize
def ticket_probability(guessed_numbers):
    # guessed numbers must be only chosen from the numbers available in a final list pool
    for n in guessed_numbers:
        if type(n) != int and (n < 49 or n > 0):
            return "Please insert whole numbers from 1 to 49 only"
    if len(guessed_numbers) != 6:
        return 'Please enter 6 numbers'
    outcomes = combinations(49, len(guessed_numbers))
    probability = 1/outcomes
    percentage_form = probability * 100

    print('''Your chances to win the big prize with the numbers {} are {:.7f}%.
In other words, you have a 1 in {:,} chances to win.'''.format(user_numbers,
                    percentage_form, int(n_combinations)))
    
test_input_1 = [2, 43, 22, 23, 11, 7]
ticket_probability(test_input_1)   

What I expected to happen:
Calculate probability of a single ticket

What actually happened:

Factorial of  49  is: 608281864034267560872252163321295376887552831379210240000000000
Factorial of  6  is: 720
Factorial of  43  is: 60415263063373835637355132068513997507264512000000000
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-14e381db3393> in <module>
     16 
     17 test_input_1 = [2, 43, 22, 23, 11, 7]
---> 18 ticket_probability(test_input_1)
     19 

<ipython-input-22-14e381db3393> in ticket_probability(guessed_numbers)
      7     if len(guessed_numbers) != 6:
      8         return 'Please enter 6 numbers'
----> 9     outcomes = combinations(49, len(guessed_numbers))
     10     probability = 1/outcomes
     11     percentage_form = probability * 100

<ipython-input-2-b09c4d38f969> in combinations(n, k)
      2 def combinations(n, k):
      3     numerator = factorial(n)
----> 4     denominator = factorial(k) * factorial(n-k)
      5     return numerator/denominator

TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

Hello All,

Could you please advise what did I skip?

Thank you in advance

Kind regards,
Malgorzata

Your factorial function is not returning any value it supposed to return integer value.

1 Like

Thank you, that’s right!

1 Like