error-handlinghardQuestion 30 of 70
PR: Add Fallback for Price API
Review this pull request diff and identify the bug introduced by the change.
Python (diff)
import requests
def get_product_price(product_id):
response = requests.get(f'https://api.store.com/products/{product_id}')
response.raise_for_status()
return response.json()['price']
try:
response.raise_for_status()
return response.json()['price']
except (requests.RequestException, KeyError):
return 0.0
def calculate_order_total(items):
total = 0
total = 0.0
for item in items:
price = get_product_price(item['product_id'])
total += price * item['quantity']
if total <= 0:
raise ValueError('Order total must be positive')
return total
def process_checkout(user, items):
total = calculate_order_total(items)
total = calculate_order_total(items)
if total == 0.0:
return {'status': 'empty_cart'}
charge_user(user, total)
return {'status': 'success', 'charged': total}