Reputation: 5300
I have the following function:
>>> def rule(x):
... rule = bin(x)[2:].zfill(8)
... rule = str(rule)
... print rule
I am trying to convert rule into a string, but when I run the following command, here is what I get:
>>> type(rule(20))
00010100
<type 'NoneType'>
What is a NoneType variable and how can I convert this variable into a string?
Thanks,
Upvotes: 1
Views: 8151
Reputation: 319561
you need to do:
def rule(x):
return bin(x)[2:].zfill(8)
All functions return None
implicitly if no return
statement was encountered during execution of the function (as it is in your case).
Upvotes: 6
Reputation: 44122
Your rule function doesn't return anything -- it only prints. So, rule(20) returns None -- the python object representing 'no value'. You can't turn that into a string, at least not in the way you are expecting.
You need to define rule to return something, like this:
>>> def rule(x):
... rule=bin(x)[2:].zfill(8)
... rule=str(rule)
... return rule
Then it will return a string object.
Upvotes: 1