ระดับกลาง
4 ตุลาคม 2569หาบั๊กให้เจอ: Django
โค้ดหนึ่งชิ้น หาบั๊กให้เจอ หรือพิสูจน์ว่าไม่มีบั๊ก
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)คลิกบรรทัดที่เป็นต้นเหตุของปัญหา
คำตัดสินของคุณ
วันละหนึ่งครั้ง คำตอบจะแสดงทันทีหลังจากนั้น
รักษาสถิติต่อเนื่องของคุณ
สร้างบัญชีเพื่อเก็บสถิติต่อเนื่องและติดตามความคืบหน้า