-1
def lucky_number(name):
  number = int(len(name) * 9)
  pr = "Hello " + name + ". Your lucky number is " + str(number)
  print(str(pr))

print(lucky_number("Kay"))
print(lucky_number("Cameron"))

output

Hello Kay. Your lucky number is 27
None
Hello Cameron. Your lucky number is 63
None

why iam geting none

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Siddesh As
  • 1
  • 1
  • 1
  • 1
    Tip: You can skip int cast here `number = int(len(name) * 9)`, because `len(name)` returns an int. Same for `str(pr)`; pr is already a str. – santo May 30 '20 at 07:51

5 Answers5

1

While every function might not have an explicit return it will have an implicit one, that is None, which incidentally is a normal Python object. Further, functions often return None explicitly.

So calling the function from within the print, without a return Value so None is returned and being printed.

To avoid this issue you should return a desired value from function.

David
  • 8,113
  • 2
  • 17
  • 36
0

Because you haven't return anything. If code is like this:

def lucky_number(name):
  number = int(len(name) * 9)
  pr = "Hello " + name + ". Your lucky number is " + str(number)
  print(str(pr))
  return "OK"

print(lucky_number("Kay"))
print(lucky_number("Cameron"))

Output:

Hello Kay. Your lucky number is 27
OK
Hello Cameron. Your lucky number is 63
OK

Calling the function in print(), prints the output of the function, in this case OK. But the string that you wanted to print is printed by the function itself, not because of the print that is calling the function.

So code like this:

def lucky_number(name):
  number = int(len(name) * 9)
  pr = "Hello " + name + ". Your lucky number is " + str(number)
  print(str(pr))

lucky_number("Kay")
lucky_number("Cameron")

Output:

Hello Kay. Your lucky number is 27
Hello Cameron. Your lucky number is 63
Charalamm
  • 1,547
  • 1
  • 11
  • 27
Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21
0

here you are printing the output:

print(str(pr))

and here you are printing none:

print(lucky_number("Kay"))

why are you printing in calling method? just call it simply, not printing.

or instead of print(str(pr)) use return str(pr) and print the output while calling the method.

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
0

try this

def lucky_number(name):
  number = int(len(name) * 9)
  pr = "Hello " + name + ". Your lucky number is " + str(number)
  return pr

print(lucky_number("Kay"))
print(lucky_number("Cameron"))
0

The function returns none after printing the statement. You can modify the code as below to use the return in your favor.

def lucky_number(name):
    number = int(len(name) * 9)
    pr = 'Hello ' + name + '. Your lucky number is ' + str(number)
    return(str(pr))

print(lucky_number('kay'))
print(lucky_number('Cameron'))

Or, you can just call the function and skip the print statement while calling to get rid of this.

def lucky_number(name):
    number = int(len(name) * 9)
    pr = 'Hello ' + name + '. Your lucky number is ' + str(number)
    print(str(pr))

lucky_number('kay')
lucky_number('Cameron')
anantdark
  • 387
  • 1
  • 2
  • 12