[Solved] Requests: retrieving data, Success or Fail. Fails base case. | SoloLearn: Learn to code for FREE!

+1

[Solved] Requests: retrieving data, Success or Fail. Fails base case.

Define a function check_success(url) that, given a URL, sends out a GET request and returns "Success" if it succeeds and "Fail" otherwise. Hint Remember that the status codes between 200 and 400 indicate that the request succeeded, while all the codes starting with a 4 signalize an error. https://code.sololearn.com/ce3SnMaMbdH7/?ref=app

7/14/2020 3:54:25 PM

Subham

21 Answers

New Answer

+2

Subham get the status code. Convert to it an int. Then check if the value is 200 <= code < 400 code needs to be greater than or equal to 200 and less than 400 Codes that are 100-199 are info Codes that are 200-299 are success Codes that 300-399 are redirects Codes 400-499 are client errors Codes 500+ are server errors https://en.m.wikipedia.org/wiki/List_of_HTTP_status_codes

+2

BTW This was supposed to be an 'and' not an 'or', and it should have been s >= 200, which is why it failed; import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if int(s) < 400 or int(s) > 200: # Needs to be 'and' not 'or'! Also s >= 200 return 'Success' else: return 'Fail'

+2

Lol it was because you're supposed to return the value not print it!

+1

Subham only imported requests module https://code.sololearn.com/c3b2VsoK9og4/?ref=app

+1

s.startswith("4") would indicate an error not a success. Haven't done this one yet, but just on a hunch try; import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if s.startswith("4") or s.startswith("5"): print('Fail') else: print('Success!')

+1

import requests def check_success(url): r = requests.get(url) if 200 <= r.status_code < 400: print('Success!') else: print('Fail')

+1

If you look back in the "Theory: Requests: retrieving data" section, it shows this code; if r: print('Success') else: print('Fail') This is probably what is wanted. It also states that if you use a response object in an if statement, it will evaluate to True if the status code starts with the digits 2 or 3, and False otherwise.

+1

Codes that should work: import requests def check_success(url): r = requests.get(url) if r: return 'Success' else: return 'Fail' def check_success(url): r = requests.get(url) if 200 <= r.status_code < 400: return 'Success' else: return 'Fail' def check_success(url): r = requests.get(url) s = str(r.status_code) if s.startswith("2") or s.startswith("3"): return 'Success' else: return 'Fail'

+1

raise_for_status() didn't work because it is only raised for certain status codes and a fail is wanted for all values outside the 200-399 range. I don't think it will raise an error for any info codes (100-199) and there may also be some in the 400-500+ range that aren't raised. I haven't been able to find the exact codes that will raise an error with it as of yet. It is typically used only for debugging though. Edit: Yes it raises for all 4XX-5XX codes per documentation.

0

it's a website question..... FEEDBACK Failed test #1 of 4. Runtime error Error: Traceback (most recent call last): File "jailed_code", line 6, in <module> install('requests') File "jailed_code", line 4, in install subprocess.call([sys.executable, "-m","pip","--disable-pip-version-chec","-q", "install", package]) File "/opt/pythonz/pythons/CPython-3.6.2/lib/python3.6/subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "/opt/pythonz/pythons/CPython-3.6.2/lib/python3.6/subprocess.py", line 707, in __init__ restore_signals, start_new_session) File "/opt/pythonz/pythons/CPython-3.6.2/lib/python3.6/subprocess.py", line 1333, in _execute_child raise child_exception_type(errno_num, err_msg) PermissionError: [Errno 13] Permission denied 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥

0

import requests def check_success(url): try: url.raise_for_status() except HTTPError: return "Fail" return "Success" didn't work as well

0

import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if s.startswith("2") or s.startswith("3"): print('Success!') else: print('Fail') didn't work

0

import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if int(s) < 400 or int(s) > 200: print('Success') else: print('Fail') doesn't work

0

import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if s.startswith("4") or s.startswith("5"): print('Fail') else: print('Success') fails

0

import requests def check_success(url): r = requests.get(url) if r.status_code < 400: print('Success') else: print('Fail') doesn't work

0

Hint: Judge by the return object of GET request

0

if r didn't work... import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if int(s) < 400 and int(s) > 200: print('Success') else: print('Fail') failed too

0

import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if int(s) < 400 and int(s) >= 200: print('Success') else: print('Fail') fails too

0

import requests def check_success(url): r = requests.get(url) s = str(r.status_code) if int(s) < 400 and int(s) >= 200: return 'Success' else: return 'Fail' succeeds

0

🤦