2

So i' trying to create a chest for a rpg game i'm working on and i'm having a really difficult time implementing probability along with making sure one item always drops on open. I was able to get the probability down but whenever i get it to work i'm unable to make the potion drop along with the weapon. Any help would be greatly appreciated! And yes my items are named from fairy tail.

I've already tried giving the items weight but even so i'm unable to make sure the potion drops


import random
-------------------------------------------------------------------------
Chestitems = [
    "constant: Potion",
    "common: Bow",
    "common: Sword",
    "common: Spear",
    "uncommon: irondragon_Bow",
    "uncommon: irondragon_Sword",
    "uncommon: irondragon_Spear",
    "rare: skydragon_Bow",
    "rare: skydragon_Sword",
    "rare: skydragon_Spear",
    "legendary: firedragon_bow",
    "legendary: firedragon_Sword",
    "legendary: firedragon_Spear"
    ]
while(True):
    OpenChest = input("Type 'open' to open the Chest!")
    if OpenChest == "open":
        print(random.choice(Chestitems))
    else:
        break

The code i have above is what i have working. Consider i'm self taught with no schooling it's really hard to find anything that helped me completely.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Laxlord2h
  • 31
  • 3
  • Are you saying you're worried the code you shared might never select the first element from `Chestitems`? Since you're using `random.choice()` on the list, there's no reason to worry about this? By the way, don't name your variables with capitals in upper camel case, your editor and other coders will think they are class names instead of variable names. – Grismar May 14 '19 at 00:52
  • I recommend looking into Python Classes. For reference, if you had a ChestItem class, with an attribute of ItemType, Name, Probability your code would be much more modular and easier to customize in the future. Here's a reference: https://www.w3schools.com/python/python_classes.asp – Baily May 14 '19 at 01:16
  • possible duplicate of https://stackoverflow.com/questions/43511941/trying-to-simulate-an-overwatch-loot-box-opening-program-in-python/ – Nic May 14 '19 at 01:23
  • Also recommend a good introductory book for programming. 'Learn Python the Hard Way' which I've just managed to download for free – Nic May 14 '19 at 07:31

1 Answers1

5

If you always want a potion plus some other item, you can do this:

print("You found a potion plus a " + random.choice(Chestitems))

Also, it might be better to arrange your common/rare/etc. items into separate lists:

common_items = ["Bow", "Sword", "Spear"]
uncommon_items = ["Irondragon Bow", "Irondragon Sword", "Irondragon Spear"]
# etc. for rare_items and legendary_items

Then, you can roll a random number from 1 to 100 to pick which list to choose from:

die_roll = random.randint(1, 100)
if die_roll < 75:
    items = common_items
elif die_roll < 90:
    items = uncommon_items
elif die_roll < 98:
    items = rare_items
else:
    items = legendary_items

print("You found a potion plus a " + random.choice(items))
John Gordon
  • 29,573
  • 7
  • 33
  • 58