Every HTTP answer starts with a status code. Say what it means.
What to build
A program that prints the reason and class of each status code.
Requirements
- Known codes: 200 OK, 201 Created, 204 No Content, 301 Moved Permanently, 302 Found, 304 Not Modified, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 500 Internal Server Error, 503 Service Unavailable.
- The class comes from the first digit: 1
informational, 2success, 3redirect, 4client error, 5server error. Any other first digit isinvalid. - Print
CODE REASON CLASS; useUnknownas the reason for a code that is not in the list.
Input
A line with n, then n codes, one per line.
Output
n lines like 404 Not Found client error.
Constraints
1 ≤ n ≤ 20; each code has 3 digits.
Example
Input:
3
200
404
503
Output:
200 OK success
404 Not Found client error
503 Service Unavailable server error
Why it works
The first digit of a status code is its family, so even a code the program has never seen can be sorted into the right class. The table only supplies the reason text.
Examples
3 200 404 503
200 OK success 404 Not Found client error 503 Service Unavailable server error
2 301 418
301 Moved Permanently redirect 418 Unknown client error
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Put the known codes in a dict from the number to its reason text.
Hint 2
The first digit gives the class: 1 informational, 2 success, 3 redirect, 4 client error, 5 server error.
Hint 3
For a code that is not in the table, print Unknown as its reason, but still print its class.
Solution
Show one correct solution (try it yourself first)
reasons = {200: "OK", 201: "Created", 204: "No Content", 301: "Moved Permanently", 302: "Found", 304: "Not Modified", 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 500: "Internal Server Error", 503: "Service Unavailable"}
classes = {1: "informational", 2: "success", 3: "redirect", 4: "client error", 5: "server error"}
n = int(input())
for _ in range(n):
code = int(input())
print(code, reasons.get(code, "Unknown"), classes.get(code // 100, "invalid"))
▶ Open this solution in the compiler, then press Visualize to watch it run step by step