Trung cấp
4 tháng 10, 2026Tìm lỗi: Django
Một đoạn mã. Tìm ra lỗi, hoặc chứng minh là không có lỗi nào.
python
1from django.core.cache import cache
2from .models import Product
3
4def get_product_price(product_id, discount_codes=[]):
5 product = Product.objects.get(pk=product_id)
6 price = product.price
7 if product.on_sale:
8 discount_codes.append('AUTO_SALE')
9 for code in discount_codes:
10 price = apply_discount(price, code)
11 return price
12
13def apply_discount(price, code):
14 rate = cache.get(f'discount:{code}', 0)
15 return price * (1 - rate)Nhấp vào những dòng gây ra vấn đề.
Phán quyết của bạn
Một lần thử mỗi ngày. Đáp án hiện ra ngay sau đó.
Giữ chuỗi ngày của bạn
Tạo tài khoản để giữ chuỗi ngày và theo dõi tiến độ của bạn.