Two Gifts, Exact Budget

Python for AIMedium+10 XP+10

Two Gifts, Exact Budget

You want to buy exactly two gifts and spend your whole budget — not a penny more or less.

Task: write has_pair(prices, budget). Return True if two different items in prices add up exactly to budget, and False otherwise.

  • "Different items" means different positions in the list. Two items with the same price are fine: [5, 5] with a budget of 10 is True.
  • One item can't be bought twice: [5] with a budget of 10 is False.
  • Prices are whole numbers.

Examples

prices = [8, 3, 12, 6]
budget = 15
returns True
prices = [5]
budget = 10
returns False
prices = [5, 5]
budget = 10
returns True

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run