2

I'm trying to learn python. I found a question saying correct this:

def main():
    assert ___ == type("Hello World").__name__
    assert ___ == isinstance("Hello World", str)
if __name__=="__main__":
    main()

I tried:

__some__={}
def main():
    assert __some__ == type("Hello World").__name__
    assert __some__ == isinstance("Hello World", str)
if __name__=="__main__":
    main()

When I run this, I'm getting AssertionError:

Traceback (most recent call last):
  Line 6, in <module>
    main()
  Line 3, in main
    assert __some__ == type("Hello World").__name__
AssertionError

I found that assert is used to specify a condition and an exception will be raised when that condition fails. I even used python tutor, but if I put assert somevariable I'm getting assertion error. I'm unable to understand to understand how to use == and assert to accomplish some task.

8
  • @user3553031 i declared a variable and kept at that place i'm getting assertion error Commented Apr 21, 2014 at 6:51
  • @user3553031 codepad link codepad.org/RBYJPssJ Commented Apr 21, 2014 at 7:00
  • 1
    I've edited the question to include the code and the traceback from the exception it is raising. In the future, please include those in your questions from the beginning! Commented Apr 21, 2014 at 7:16
  • @saimadan you understand that your task is to find the right thing to replace ___? Have you tried running the expressions to see what values they give? Commented Apr 21, 2014 at 7:16
  • 1
    @saimadan then why not try running e.g. print(isinstance("Hello World", str)) to see what you get? Or read the docs for isinstance? Or read up on assertions in Python? Commented Apr 21, 2014 at 7:24

1 Answer 1

4

The code that you posted on codepad.org is

__some__={}
def main():
    assert __some__ == type("Hello World").__name__
    assert __some__ == isinstance("Hello World", str)
if __name__=="__main__":
    main()

type("Hello World").__name__ is 'str' and __some__ is {}, so of course they don't match. Likewise, isinstance("Hello World", str) is True, so it doesn't match either. Your conditions are false, so the assertions fail and throw AssertionError. If on the other hand, you tried assert 'str' == type("Hello World").__name__, you'd get no exception because that comparison is true.

Also, you shouldn't declare your own variables using names like __foo__. By convention, those are reserved for special variables created by Python.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot can you suggest some sources to learn python 2.7 other than documentation
can you please explain a bit what that statements are doing actually i did not understand the type("hello world").__name__ .what is .__name__ and what is it doing
i understood now from now on i will use the command line to understand any statements thanks a lot
assert False == 'c' in 'apple' i'm getting assertion error for this can you help i think this is sort of precedence issue
(2 + 3)*4 i would do this
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.