DustyHalo
DustyHalo

Reputation: 135

Input as object python 3

So I'm making a kind of RPG program for me and my friends and I'm working on a kind of market system, the user will walk around to different shops and purchase items or sell their own items for gold which they can use to buy health potions, better gear and weapons, then go into the dungeon, kill some things, get more gold and experience to level up ect ect. So, I figured a way to do this really simply: Have an inventory for each shop, print to the user what is in the inventory and ask what they want to purchase. It checks if it is in the inventory then shows the user how much gold it will cost and ask if they want to buy it. Anyway, here is my problem: If I have an input as x, say "sword" and I define (def sword():) it above to say the cost. Is there any way for me to take the input and go x() for a define instead of having to put down sword(), shield(), axe() and so on everytime?

Upvotes: 2

Views: 57

Answers (1)

iz_
iz_

Reputation: 16613

Use a dictionary:

costs = {'sword': 100, 'shield': 80, 'axe': 60} # or whatever the actual costs are

selection = input()
try:
    cost_of_selection = costs[selection]
except KeyError:
    print('Invalid selection')

Upvotes: 1

Related Questions