-1

I created a lottery number generator with Python 3.7. It shows however None at the end of each try. Here's my code.

import random


def lotto_gen():
    n = 1
    while n < 7:
        print(random.randint(1, 45), end='\t')
        n += 1
    return


for numbers in range(100):
    print(lotto_gen())

And the result goes like this:

6   12  42  37  13  44  None
36  31  32  41  4   30  None
20  31  38  42  14  19  None
8   18  29  22  34  29  None
26  34  15  1   20  38  None
10  17  28  35  22  38  None
23  34  42  22  4   43  None
25  16  17  36  17  4   None
44  8   20  1   43  43  None
29  32  9   2   8   5   None
16  44  35  17  42  10  None
5   1   39  28  21  40  None
35  25  12  31  23  21  None
13  25  9   10  41  7   None
12  34  14  36  27  5   None
32  30  12  5   41  14  None
23  30  5   30  7   9   None
38  25  6   17  17  20  None
12  1   13  10  30  32  None
15  1   3   23  28  6   None
1   2   24  33  36  31  None
28  13  42  39  9   39  None
41  44  2   9   41  34  None
25  19  30  26  8   44  None
39  36  44  4   22  7   None
7   44  29  38  1   8   None
37  6   44  6   41  11  None
29  29  23  40  23  36  None
25  39  30  40  40  4   None
28  14  33  4   15  34  None
41  35  7   26  30  24  None
10  34  26  45  12  10  None
32  6   45  16  24  18  None
14  7   8   26  32  4   None
22  43  40  3   20  31  None
6   42  38  11  18  20  None
6   40  5   18  25  29  None
37  19  26  19  45  41  None
39  8   17  19  17  22  None

I want to remove that None bool type. Can someone tell me how can I edit my code?

harryghgim
  • 1,340
  • 4
  • 12
  • 33
  • Your function implicitly returns `None`, and it is being printed in the second loop. This was already covered multiple times (see, for example, [Function returns None without return statement](https://stackoverflow.com/q/7053652/7851470)). Voting to close as a "typo". – Georgy Feb 25 '20 at 11:10
  • 1
    ``lotto_gen`` itself already prints the random numbers, and returns ``None``. That means the value of ``lotto_gen()`` is ``None``, which you print in the for loop. – MisterMiyagi Feb 25 '20 at 11:10
  • Just call the function, without printing it because it is already doing the printing inside. Change `print(lotto_gen())` to `lotto_gen()`.... – Tomerikoo Feb 25 '20 at 11:11

2 Answers2

0

This is one approach.

Ex:

import random

def lotto_gen():
    return "\t".join(str(random.randint(1, 45)) for  _ in range(6))

for numbers in range(100):
    print(lotto_gen())
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Rakesh has given the correct answer, but I would like to explain why your code isn't working. The problem seems to be that for a particular iteration, your code is only able to generate 6 random numbers. Take note, that you have initialized n=1, inside the function lotto_gen() and as the condition for executing the while loop is n<7, the code inside lotto_gen() executes only 6 times.

Now the reason why you receive None at the end is because you are trying to print the value returned by lotto_gen, but take note, that the return field inside your code's function is empty, hence None is returned by the function and hence that gets printed.

So for correcting the code you only need to initialize n as n=0, and to remove the appearance of the none, don't call the function inside a print statement, and create a list which contains the 7 values of each iteration and return it. So, you'll need to modify the code in this manner:

import random

def lotto_gen():
    n = 0
    a=[]
    while n < 7:
        a.append(random.randint(1, 45))       
        n += 1
    return a


for numbers in range(100):
    print(lotto_gen())

You can use this approach too and my code will execute faster as well! :P

Saurav Saha
  • 745
  • 1
  • 11
  • 30