Attempting to assign a boolean to multiple randomly chosen variables

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 answer

  • answered 2019-08-13 03:50 illiteratecoder

    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)