Comp Sci 2026 Computer Science 2026

Assignment: Haggle at the Shop

Due 11/25 at 11:59pm

In this project, you will extend the Simple Shop assignment with haggling mechanics. Now you can negotiate prices with the shopkeeper, but the further your offer is from the asking price, the more likely they’ll refuse.

Your program must meet the following specifications:

For example:

Welcome to the shop! You have 100 gold.

=== SHOP INVENTORY ===
- sword
- shield
- boots
- helmet

Pick an item (or type 'exit'): sword
The shopkeeper asks for 50 gold.
Your offer: 40
The shopkeeper considers... and ACCEPTS!

You bought the sword for 40 gold!
You now have 60 gold.

Pick an item (or type 'exit'): shield
The shopkeeper asks for 30 gold.
Your offer: 10
The shopkeeper considers... and REFUSES!

Pick an item (or type 'exit'): boots
The shopkeeper asks for 20 gold.
Your offer: 20
The shopkeeper considers... and ACCEPTS!

You bought the boots for 20 gold!
You now have 40 gold.

Pick an item (or type 'exit'): exit

=== PERSONAL INVENTORY ===
- sword
- boots

Total spent: 60 gold
Gold remaining: 40 gold

Haggling Logic

The shopkeeper’s acceptance is based on how far your offer is from the base price.

For example, if the item costs 50 gold and your offer is 48, the shopkeeper has a 90% chance of accepting.

However, if the item costs 50 gold and your offer is 35, the shopkeeper only has a 40% chance of accepting.

The shopkeeper’s acceptance logic is already implemented using random.randint() to generate a random number and compare it to the acceptance chance.

Error Handling

When asking the user for their offer, it’s important to make sure they actually type a number! If the user enters something that’s not a number (like "twenty"), the program should show an error message and ask again.

You can do this using a while True loop and a try...except block to catch any errors:

while True:
    offer_input = input("Your offer: ")
    try:
        haggle_price = int(offer_input)
        break  # It's a number, exit the loop!
    except ValueError:
        print("Please enter a valid number for your offer.")

This way, your program won’t crash, and will keep asking until the user enters a valid number!

Example Code

import random

# Item names and their prices
item_names = ["sword", "shield", "boots", "helmet"]
item_prices = [50, 30, 20, 25]

# Player inventory and gold
inventory = []
gold = 100

print("Welcome to the shop! You have 100 gold.")
print(f"Available items: {', '.join(item_names)}")

def shopkeeper_accepts(haggle_price, base_price):
    """Decides if the shopkeeper accepts the haggled price."""
    difference = abs(haggle_price - base_price)
    if difference == 0:
        accept_chance = 100
    elif difference <= 5:
        accept_chance = 90
    elif difference <= 10:
        accept_chance = 70
    elif difference <= 15:
        accept_chance = 40
    else:
        accept_chance = 10
    return random.randint(1, 100) <= accept_chance

while True:
    print()
    choice = input(f"Pick an item (or type 'exit'): ").lower()
    
    if choice == "exit":
        break
    
    # Get the item's base price
    item_index = item_names.index(choice)
    base_price = item_prices[item_index]
    
    print(f"The shopkeeper asks for {base_price} gold.")
    
    # TODO: Get haggle price from user
    
    # Use function for shopkeeper decision
    if shopkeeper_accepts(haggle_price, base_price):
        print("The shopkeeper considers... and ACCEPTS!")
        # TODO: Complete transaction
        print(f"You bought the {choice} for {haggle_price} gold!")
        print(f"You now have {gold} gold.")
    else:
        print("The shopkeeper considers... and REFUSES!")

# TODO: Print final inventory with prices, total spent, and remaining gold
Additional Challenges (not required)

When you’re happy with your program, upload the .py file to ThinkWave.