0
0
FastAPIframework~10 mins

Numeric validation (gt, lt, ge, le) in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to require the query parameter 'age' to be greater than 18.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(age: int = Query(..., gt=[1])):
    return {"age": age}
Drag options to blanks, or click blank then click option'
A18
B0
C21
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using ge instead of gt changes the condition to 'greater or equal'.
Setting the value to less than 18 allows younger ages.
2fill in blank
medium

Complete the code to require the query parameter 'price' to be less than 100.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/products/")
async def get_products(price: float = Query(..., lt=[1])):
    return {"price": price}
Drag options to blanks, or click blank then click option'
A50
B100
C150
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using le instead of lt allows price equal to 100.
Setting the value higher than 100 does not restrict prices below 100.
3fill in blank
hard

Fix the error in the code to require 'quantity' to be greater or equal to 1.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/orders/")
async def get_orders(quantity: int = Query(..., [1]=1)):
    return {"quantity": quantity}
Drag options to blanks, or click blank then click option'
Ale
Bgt
Cge
Dlt
Attempts:
3 left
💡 Hint
Common Mistakes
Using gt excludes 1 from valid values.
Using le or lt sets upper limits instead.
4fill in blank
hard

Fill both blanks to require 'rating' to be between 1 and 5 inclusive.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/reviews/")
async def get_reviews(rating: int = Query(..., [1]=1, [2]=5)):
    return {"rating": rating}
Drag options to blanks, or click blank then click option'
Age
Bgt
Cle
Dlt
Attempts:
3 left
💡 Hint
Common Mistakes
Using gt or lt excludes boundary values.
Swapping the order of parameters causes errors.
5fill in blank
hard

Fill all three blanks to require 'score' to be greater than 0, less than or equal to 100, and default to 50.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/scores/")
async def get_scores(score: int = Query([1], gt=[2], [3]=100)):
    return {"score": score}
Drag options to blanks, or click blank then click option'
A50
B0
Cle
Dlt
Attempts:
3 left
💡 Hint
Common Mistakes
Using lt instead of le excludes 100.
Setting default value outside the valid range causes errors.