-1

I'm trying to organise a drop system for mining in a text based rpg i'm creating with python, the idea is basically once you destroy a rock you have a chance of getting loot, the problem is i need some way of making a string into a variable then being able to print the string to say something like "You now have 2 Stone" this is what I have so far:

stone = 1
drops = ["stone","none"]

drop1 = random.choice(drops)

if drop1 == "none":
    print("You get nothing")
else:
    drop1 = eval(drop1 + 1)
    print("You now have",eval(drop1)," "+drop1)
ManxFox
  • 1
  • 1
  • 5
  • Use a dict with the keys of your choices and the values of whatever object you want. – Keozon May 12 '16 at 11:55
  • you can make the value anything you want. Any object. So, if you want two values, just make the value a list, or better yet, design an inventory object that handles it all for you. I'm guessing you're making this program for learning purposes, anyway, so learning to make a pythonic inventory object with methods that can update your inventory based on a string, would be advantageous. – Keozon May 12 '16 at 12:04

1 Answers1

1

Use a dictionary:

loot = {
    "stone": 1
}

drops = ["stone", "none"]

drop1 = random.choice(drops)

if drop1 == "none":
    print("You get nothing")
else:
    loot[drop1] += 1
    print("You now have {} {}".format(drop1, loot[drop1]))
  • Let me try and explain, I can't use a dictionary because the game is saving data via the use of variables to a notepad file, I need to be able to store all the values of each material to a single variable and be able to call that variable from the list of materials in the game, the problem with using dictionaries is I can't find a way to store those items in the dictionary as variables, meaning i can[t really use a dictionary for this, I need to find out how to call a variable from a string that's used in the list. – ManxFox May 12 '16 at 12:25
  • What do you mean by "saving data via the use of variables to a notepad file"? Do you write a text file? You could serialize a dictionary to JSON, for example. That's plain text. –  May 12 '16 at 12:27
  • It's formatting the text file such as: Potato #PlayerName then on the next line 10 #PlayerHealth – ManxFox May 12 '16 at 12:29
  • so the idea is I want about 10 variables for different materials (iron ore, stone, copper ore, tin ore, etc) with values that will be saved and loaded from the text file BUT also be updated via the drop system (which is a list of constantly changing strings based on whatever rock the player encounters – ManxFox May 12 '16 at 12:30
  • Please write a new question about this. Your question above is answered. Maybe you should think about how you persist your application state. –  May 12 '16 at 12:31
  • the problem I'm facing is getting python to understand that if ("stone") appears in the list of drops as the drop that's chosen, I want it to update the stone variable by adding a piece of stone to it – ManxFox May 12 '16 at 12:32
  • Alright, I'll try and explain it better in a different question – ManxFox May 12 '16 at 12:32