BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

TypeError: Float Object Cannot Be Interpreted as an Integer

Screen Link:

My Code:

def probability_less_6(numbr):
    numbr_combn = combinations(6, numbr)
    succ_outcme = 49 - numbr_combn
    total_no_succ_outcme = numbr_combn * succ_outcme
    prob = (total_no_succ_outcme / combinations(49, numbr_combn)) * 100 
    print('''The likelihood of your matching {:} numbers in your lottery ticket is {:.2f}%.'''.format(numbr, prob))
 
nmbr in [2, 3, 4, 5]:
    probability_less_6(nmbr)

What I expected to happen:
Probability for matching 2 to 5 numbers would be printed on the screen.

What actually happened:

TypeError: 'float' object cannot be interpreted as an integer

The factorial and combinations functions worked well in previous steps. Why are 2 to 5 matches considered floats and how to change this?
Below is a copy of my code for this project:
Winning+the+Lottery+Probability+App-Copy2.ipynb (16.5 KB)

Click here to view the jupyter notebook file in a new tab

It’s important to pay attention to and share the entire error message. Your probability_less_6 function calls the combinations function that call the factorial function. Looking at the error message, we can see that the error is raised in the factorial function:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-aca7167c69e5> in <module>
      1 for nmbr in [2, 3, 4, 5]:
----> 2     probability_less_6(nmbr)

<ipython-input-12-562281b67545> in probability_less_6(numbr)
      7     total_no_succ_outcme = numbr_combn * succ_outcme
      8     print(total_no_succ_outcme)
----> 9     prob = (total_no_succ_outcme / combinations(49, numbr_combn)) * 100
     10     print('''The likelihood of your matching {:} numbers in your lottery ticket is {:.2f}%.'''.format(numbr, prob))

<ipython-input-1-ba3ae0657370> in combinations(n, k)
      6 
      7 def combinations(n, k):
----> 8     result = factorial(n) / (factorial(k)*(factorial(n - k)))
      9     return result
     10 #Using the factorial and combinations functions, we will calculate

<ipython-input-1-ba3ae0657370> in factorial(n)
      1 def factorial(n):
      2     final_product = 1
----> 3     for i in range(n, 0, -1):
      4         final_product *= i
      5     return final_product

TypeError: 'float' object cannot be interpreted as an integer

So the problem is the range function receives a float, which is unacceptable to this function. But where does this float come from if the probability_less_6 function only receives int? Let’s investigate.

If you print each variable in the probability_less_6 function and its type, you can see the exact moment that you start dealing with a float.

def probability_less_6(numbr):
    print(numbr, type(numbr))
    numbr_combn = combinations(6, numbr)
    print(numbr_combn, type(numbr_combn))
    succ_outcme = 49 - numbr_combn
    print(succ_outcme, type(succ_outcme))
    total_no_succ_outcme = numbr_combn * succ_outcme
    print(total_no_succ_outcme, type(total_no_succ_outcme))
    prob = (total_no_succ_outcme / combinations(49, numbr_combn)) * 100 
    print('''The likelihood of your matching {:} numbers in your lottery ticket is {:.2f}%.'''.format(numbr, prob))

This is the output before the error:

2 <class 'int'>
15.0 <class 'float'>
34.0 <class 'float'>
510.0 <class 'float'>

You can see that from the numbr_combn variable on you’re dealing with float variables. In fact, you don’t even have to print the variable type to see that.

So we know the combinations function returns a float. But why? Because this function returns the result of a division and divisions in python always return floats.

For instance, if you run 4/2, the result is 2.0. If you run int(4/2), the result is 2.

If you change the combinations function to print the nominator and denomiator of the division, you’ll see that they are int variables, but the result of their division (which is returned by the function) is a float.

The function with these changes:

def combinations(n, k):
    denominator = (factorial(k)*(factorial(n - k)))
    print(denominator)
    nominator = factorial(n)
    print(nominator)
    result = nominator / denominator
    return result

print(combinations(6, 5))

The outcome:

120
720
6.0

So what you can do is to transform all the values in the probability_less_6 function to int or you can make the combinations function return int, like this:

def combinations(n, k):
    result = factorial(n) / (factorial(k)*(factorial(n - k)))
    return int(result)

With these modifications, here’s the output I got from the probability_less_6 function:

2 <class 'int'>
15 <class 'int'>
34 <class 'int'>
510 <class 'int'>
The likelihood of your matching 2 numbers in your lottery ticket is 0.00%.
3 <class 'int'>
20 <class 'int'>
29 <class 'int'>
580 <class 'int'>
The likelihood of your matching 3 numbers in your lottery ticket is 0.00%.
4 <class 'int'>
15 <class 'int'>
34 <class 'int'>
510 <class 'int'>
The likelihood of your matching 4 numbers in your lottery ticket is 0.00%.
5 <class 'int'>
6 <class 'int'>
43 <class 'int'>
258 <class 'int'>
The likelihood of your matching 5 numbers in your lottery ticket is 0.00%.

No error was raised.

I hope this helps you.

1 Like