Beno Terzic
Beno Terzic

Reputation: 43

How to match a sublist with it's list using while using random?

I might explain this weird and I apologize for that. Me and my friend are creating a D&D randomizer, and whenever the main event is randomized, the sub-event of that main event also gets randomized, but the sub-list MUST match its paired Main event.

For example, "Caravan(Main Event)" and "(Sub Event)Prison Transport" is supposed to be paired. "Humans In Black Cloaks(Main Event)" and "Non-Hostile Cultists(Sub Event) is supposed to be paired. But when I run the randomizer, sometimes it will roll "Humans In Black Cloaks" with "Prison Transport" when those two aren't supposed to be paired. This is my entire code for the whole thing. Whoever takes the time to help me, you're the best. New to Stack Overflow.

import random
# Random Events
re1 = "Humans In Black Cloaks" re2 = "Caravan"

Random_Events = [re1, re2]



# Random Event 1 Subs 
re1_sub1 = "Hostile Cultists Of A Dark God"

re1_sub2 = "Non-Hostile Cultists"

re1_sub3 = "Monsters Trying To Travel Peacefully"

re1_sub4 = "Wandering Magic Merchant"

re1_sub5 = "Terror Stricken Peasants"

re1_sub6 = "Travelers Who like Black Cloaks"

# Random Event 2 Subs 
re2_sub1 = "Merchant Caravan"

re2_sub2 = "Peasant Traders"

re2_sub3 = "Prison Transport"

re2_sub4 = "Military Caravan"

re2_sub5 = "Traveling Entertainers"

re2_sub6 = "Nomadic Tribe"

# Lists 
re1_sublist = [re1_sub1, re1_sub2, re1_sub3, re1_sub4, re1_sub5, re1_sub6] 
re2_sublist = [re2_sub1, re2_sub2, re2_sub3, re2_sub4, re2_sub5, re2_sub6] 


if random.choice(Random_Events) == re1:
print(random.choice(re1_sublist)) 
else:
print(random.choice(re2_sublist))

print(random.choice(Random_Events))

Upvotes: 0

Views: 143

Answers (2)

user8288709
user8288709

Reputation:

You are expecting a certain event to pair up with another which is predetermined to occur. Hence cannot be random.

if you want to pair the 2 events change the code as below

if random.choice(Random_Events) == re1:
    print(re1_sublist[1])#extract the string from the list
else:
    print(re1_sublist[2])

print(random.choice(Random_Events))

Upvotes: 0

mapeters
mapeters

Reputation: 1117

You need to only select a random event from Random_Events once. Currently, you are selecting one random event to determine the sublist (the if random.choice(Random_Events) == re1: line), and a separate random event to actually print out (the print(random.choice(Random_Events)) line). Also, using a dict to map the events to sublists would simplify things.

Following those guidelines, you should end up with something like this:

event_dict = {
    re1 : [re1_sub1, re1_sub2, re1_sub3, re1_sub4, re1_sub5, re1_sub6],
    re2 : [re2_sub1, re2_sub2, re2_sub3, re2_sub4, re2_sub5, re2_sub6]
}

# Only select from Random_Events once, storing its value so we can use it
# both to determine the sublist and to print it out
random_event = random.choice(Random_Events)
sublist = event_dict[random_event]
random_sublist_item = random.choice(sublist)

print(random_sublist_item)
print(random_event)

Upvotes: 1

Related Questions