0

I wanted to create a random numerical password generator.So, I made one. It works correctly, just like the way i wanted it to work.But there is an extra word printed in the console.

def randgen():
    # import the 'random' module
    import random
    # create variables for each digit in the password
    no1 = 0
    no2 = 0
    no3 = 0
    no4 = 0
    no5 = 0
    no6 = 0
    no7 = 0
    no8 = 0
    no9 = 0
    no10 = 0
    # generate random number for each digit
    # in each iteration
    for x in range(10):
        if x == 0:
            no1 = random.randrange(0, 9, 1)
        elif x == 1:
            no2 = random.randrange(0, 9, 1)
        elif x == 2:
            no3 = random.randrange(0, 9, 1)
        elif x == 3:
            no4 = random.randrange(0, 9, 1)
        elif x == 4:
            no5 = random.randrange(0, 9, 1)
        elif x == 5:
            no6 = random.randrange(0, 9, 1)
        elif x == 6:
            no7 = random.randrange(0, 9, 1)
        elif x == 7:
            no8 = random.randrange(0, 9, 1)
        elif x == 8:
            no9 = random.randrange(0, 9, 1)
        elif x == 9:
            no10 = random.randrange(0, 9, 1)

    # print all the digits concatenated
    print(no1, no2, no3, no4, no5, no6, no7, no8, no9, no10)


# just a cherry on the cake
print("Type 'y' when ready to generate random password.")
print("Type anything else to cancel.")
if input() == "y":
    print(randgen())
else:
    pass

Result:

Type 'y' when ready to generate random password.
Type anything else to cancel.
y
2 2 7 1 3 1 6 3 0 7
None

There's the word 'None' printed.I don't want it to appear.Can anyone help?

  • 1
    Change `print(randgen())` to `randgen()`. Or change the last `print` inside `randgen()` to a `return` statement. – khelwood Feb 24 '20 at 16:08
  • As khelwood pointed out above, you have an extra `print`. In Python, if a function doesn't have an explicit `return` call, it will return `None`. And that's what `print(randgen())` is printing. – RogB Feb 24 '20 at 16:12
  • @khelwood I changed the `print(randgen())` to just calling the function like `randgen`.The unwanted None doesn't appear.But @RogB stated that without a explicit return in an function, it will print None.I still did not add a return statement, but the None is non existent. Am I missing something here? –  Feb 24 '20 at 16:39
  • @Dharmendhar RogB said that if your function does not have a return statement then it will *return* None. But you don't have to *print* that. Returning and printing are completely different things. – khelwood Feb 24 '20 at 19:05

0 Answers0