Merchant menu help

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.")

This snippet has few errors, first of all you probably want to use buyer and not vampire in sell_item

This is because the items are:

self.items = {"1. blood vial": 5, "2. artifact": 140, "3. ancient text": 100}

If i was trying to check if i have an artifact, then code runs:

if "artifact" in self.items:

But self.items doesnt have artifact. It has 2. artifact
Thats why it always returns that it doesnt have it.

So remove the bullet points:
2. artifactartifact
Use a for loop instead to print each item and add the bullets when printing

1 Like

@QuantumCodes was fast so I had to delete part of my answer.
The problem is partly what @QuantumCodes says and fundamentally you need to test with ‘in self.items.keys()’ not just self.items

1 Like

The in operator by default checks the keys. It never checks items in a dictionary

2 Likes

@GoodIdeas please mark @QuantumCodes as solution before going back coding :slight_smile:

@QuantumCodes , you are right i am getting confused among languages (as i am now using one where that needs keys …)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.