This forum is now read-only. Please use our new forums at discuss.codecademy.com.

How to use APIs with Python Forum

View Course » View Exercise

545 points
Picture
Submitted by
Krishan Vadher
about 2 years ago

My answer - this can't be right surely?

import requests

response = requests.get('http://placekitten.com/')

# Add your code below!
response.status_code(200)
print status_code

I'm sure this is worng, but the excercise sitll passes it. Any idea what the answer is.


2 votes

permalink

do this instead:

print response.status_code

it will show "200" as it is demanded

380 points
329fcaead4b30595c2c0ff30b928e683?s=140&d=retro
Submitted by
RonRiley
about 2 years ago


0 votes

permalink

As RonRiley said, the correct answer is just to print out the status code.

As for what's going on, I'm not sure. The response variable is actually an object, and it has an attribute named status_code, which is an integer. You can "read" the attribute of an object with the following syntax:

object.attribute

And methods are invoked similarly.

object.method(args)

But the response object doesn't contain any method named status_code, as the following test shows:

import requests
response = requests.get("http://placekitten.com/")
try:
    #Trying to invoke the method.
    response.status_code(200)
except NameError:
    print("There is no method called status_code.")

Or, for a more direct approach, just do this:

import requests
response = requests.get("http://placekitten.com/")
print(dir(response))

The dir() function will return a list of all the methods and attributes of a given object. Methods are always listed before attributes, and there is no method called status_code.

So it is probably a Codecademy glitch, but again, I'm not sure.

848 points
252f630586b97efb5f56a6b9ea92d511?s=140&d=retro
Submitted by
DarkxCoder
about 2 years ago