[Python] Why is my code only assigning a certain variable sometimes, rather than everytime the...

Discussão em 'Python' iniciado por Stack, Agosto 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a small hobby project where I try to create a government for a fictional world. So starting out, I have an assembly of 250 people, with many different details that are being generated for each one of them and one of these things being generated is the role of said person. Most of the roles should just be MPs, but there are a few roles that should only be assigned to a certain amount of people, for example the role of the President should only be assigned to one person.

    Now, I already have all of my code working without errors, but I notice that when running the code, sometimes it generates the role of president for 1 person and other times it doesn't generate the role at all! I want to know what in my code is causing this to happen.

    So in short, I want the roll of president to always be generated and assigned to a random person in the assembly, but my current code is preventing that from happening sometimes and doesn't assign the role at all.

    Here is my current code. main.py:

    import random
    import character_details as charDetails

    def legislative():
    legislative_size = 250
    return legislative_size

    def createCharacter():
    legislative_size = legislative()
    president_index = random.randint(0, legislative_size - 1)

    print(f"President will be assigned to Member #{president_index + 1}") # Debug line

    for i in range(legislative_size):
    # Assign role
    if i == president_index:
    charRoll = "President"
    else:
    charRoll = random.choice(charDetails.roleInGov)

    # Gender and Age group
    gender = random.choice(charDetails.genders)

    if i == president_index:
    # Assign age group with 15% chance of being young
    if random.randrange(100) < 15:
    ageChoice = "Young"
    else:
    ageChoice = random.choice(["Middle-Aged", "Old"])
    else:
    ageChoice = random.choice(charDetails.ages)

    if ageChoice == "Young":
    age = random.randrange(21, 39)
    elif ageChoice == "Middle-Aged":
    age = random.randrange(40, 64)
    elif ageChoice == "Old":
    age = random.randrange(65, 75)
    else:
    print("Age is not found")
    age = "Unknown"

    # Name generation
    if gender == "Male":
    personName = random.choice(charDetails.male_names)
    elif gender == "Female":
    personName = random.choice(charDetails.female_names)
    else:
    personName = "Unknown"

    personLastName = random.choice(charDetails.last_names)
    character_traits = ["Ambitious", "Brave", "Deceitful", "Just", "Lustful", "Greedy"]
    characterTraits = random.choice(character_traits)

    print("Member Number: #{}".format(i + 1))
    print("Role: {}".format(charRoll))
    print("Name: {} {}".format(personName, personLastName))
    print("Gender: {}".format(gender))
    print("Age: {}".format(age))
    print("Traits: {}".format(characterTraits))
    print("")

    createCharacter()


    character_details.py:

    roleInGov = ["MP", "Minister", "Speaker of the Assembly", "Party Leader", "Leader of Party Faction"]
    male_names = ["Thomas", "Tony", "Alec", "Steven", "James", "Alexander", "Marcus", "Zaine", "Jacob", "Paul", "Liam", "Donald", "Herbert", "Petr", "David", "Anton", "Ronald", "Hadron", "Gilligan", "Omar"]
    female_names = ["Alexis", "Jamie", "Sarah", "Ciara", "Monica", "Justine", "Marla", "Madalaine", "Janine", "Petra", "Hannah", "Fiemke", "Olga", "Jillian", "Georgia", "Atlanta", "Morigan", "Gina", "Anna", "Lee"]
    last_names = ["Margus", "Neeglin", "Alethis", "Saratoga", "Ramin", "Larmin", "Jahigan", "Mastrul", "Prima", "Questra", "Felstra", "Ulgan", "Cancea", "Stermenta", "Polkumta", "Indrah", "Ylvin", "Klarinitin", "Hartcin", "Komboto"]
    genders = ["Male", "Female"]
    ages = ["Young", "Middle-Aged", "Old"]

    Continue reading...

Compartilhe esta Página