Question:
This may be an idiotic question but that’s fine. I’m having some trouble with my NPC merchant menu for a text RPG I’m creating. My problem is that it always outputs the message that the merchant doesn’t have the item. I’ve tooled around with it for a bit and I am completely and utterly stumped, thanks to everyone that helped in advance!
Repl link:
https://replit.com/@GoodIdeas/Crimson-Intent?v=1
class Merchant:
def __init__(self, name, items):
self.name = name
self.items = items
def display_items(self):
print(" ")
print("You exit the town and begin walking towards a seemingly uninhabited cabin. You open the door to find a old man hunched over a counter with a wall of oddities behind him.")
print(" ")
print(f"Old man: Hail traveler, Welcome to {self.name}!")
print("Take a look at my wares. Let me know if anything catches your eye.")
print(" ")
for item, price in self.items.items():
print(f"{item.capitalize()}: {price} gold")
def sell_item(self, item, quantity, buyer):
if item not in self.items:
print(f"Appologies traveler, we currently don't sell {item}s.")
print("You exit the store and make your way back to town with a visible look of disappointment at the news they didnt have your requested item.")
elif self.items[item] * quantity > buyer.resources["gold"]:
print(f"Sorry, you don't have enough gold to buy {quantity} {item}(s).")
print("You exit the store and make your way back to town, angered and embarrased at the fact you did'nt have enough gold.")
else:
self.items[item] -= quantity
vampire.resources["gold"] -= self.items[item] * quantity
vampire.inventory.add_item(item, quantity)
print(f"You have purchased {quantity} {item}(s) for {self.items[item] * quantity} gold.")
# create a new instance of the Merchant class with some items for sale
merchant = Merchant("Fangs & Favors", {"1. blood vial": 5, "2. artifact": 140, "3. ancient text": 100})
merchant.display_items()
print(" ")
choice = input("Looking for anything in particular?: ")
# display the items for sale
merchant.display_items()
# sell an item to the player
if choice == "1":
merchant.sell_item("blood vial", 2, vampire)
elif choice == "2":
merchant.sell_item("artifact", 2, vampire)
elif choice == "3":
merchant.sell_item("ancient text", 2, vampire)
else:
print(" ")
print("My appologies traveler, we do not currently have that item. However we do get new shipments weekly so check back soon!")
print("You exit the store and make your way back to town with a visible look of disappointment at the news they didnt have your requested item.")