1

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,

SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Sep 10, 2009 at 20:32

2 Answers 2

6

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).

answered Sep 10, 2009 at 20:34
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Sep 10, 2009 at 20:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.