0

I'm attempting to build a random character generator for an RPG. I am attempting to Generate two skills from a select list, which if selected need to return true in the Common list.

I'm pretty new, so this may be a problem with inexperience.

What I did try was using instead of SkillChoice[0] = True I used (SkillChoice[0]).append(2) to add the modifier to a list, but with this method it cannot check if that skill was chosen.

import random

# Class
Barbarian = True

# Skills
Athletics_P = False
Acrobatics_P = False
Alchemy_P = False
Lockpicking_P = False
Sneak_P = False

if Barbarian:
    ClassSkills = (Athletics_P, Acrobatics_P, Alchemy_P, Lockpicking_P, 
Sneak_P)
    SkillChoice = random.sample(ClassSkills, 2)
    SkillChoice[0] = True
    SkillChoice[1] = True

# print check
if Athletics_P:
    print("Athletics")
if Acrobatics_P:
    print("Acrobatics")
if Alchemy_P:
    print("Alchemy")
if Lockpicking_P:
    print("Lockpicking")
if Sneak_P:
    print("Sneak")

The expected result would be it displaying two random skills, confirming that it can be checked if the variable was "True"

Actual results in nothing displaying, which leads me to believe what is actually happening is that the skills are not changing values.

No errors are displaying.

  • 1
    When you write `ClassSkills = (Athletics_P, Acrobatics_P, Alchemy_P, Lockpicking_P, Sneak_P)` that puts the values of the variables in the tuple. So it's equivalent to `(False, False, False, False, False)` – jeremye Aug 13 '19 at 03:44
  • You don't change the original variables when you do `SkillChoice[0] = True` or `SkillChoice[1] = True`. – Austin Aug 13 '19 at 03:49
  • Related: [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4518341) – wjandrea Aug 13 '19 at 03:54

1 Answers1

3

When you write ClassSkills = (Athletics_P, Acrobatics_P, Alchemy_P, Lockpicking_P, Sneak_P) that puts the values of the variables in the tuple, so it's equivalent to ClassSkills = (False, False, False, False, False).

Instead, we'd like a better way of associating the names of your skills with their value (True or False). We can use a dict for this (the following is an example of similar, but not identical functionality that you indicated in your example code):

skills = {
    'Athletics': False,
    'Acrobatics': False,
    'Alchemy': False,
    'Lockpicking': False,
    'Sneak': False
}

# Select two skills at random to set to True.
choices = random.sample(skills.keys(), 2)
for choice in choices:
    skills[choice] = True

# Print out only the skills that are set to True.
for skill in skills.keys():  # `for skill in skills:` is the same
    if skills[skill]:
        print(skill)
jeremye
  • 1,368
  • 9
  • 19
  • Wow, Thanks for the Awesome, and fast response! It really helped, I definitely have to learn more about Dictionary's they seem like a good way to reduce alot of my other work on this little project of mine! – Learning by doing Aug 13 '19 at 05:06
  • @Learningbydoing I'm glad the answer helped, best of luck with your project! You can accept the answer to indicate that it has been resolved. – jeremye Aug 14 '19 at 02:06