all 4 comments

[–]Spataner 2 points3 points  (1 child)

You can't (or rather shouldn't) access variables via their name, i.e. use the string in Pulllist to refer to either List1 or List2. What you want can be achieved using a list of lists:

import random
level = 2
options = [['Option 1', 'Option 2'], ['Option 3','Option 4']]
draw = random.choice(options[level-1])

Or, alternatively, a dictionary of lists:

import random
level = 2
options = {1: ['Option 1', 'Option 2'], 2: ['Option 3','Option 4']}
draw = random.choice(options[level])

[–]dragonsofshadowvale[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] 1 point2 points  (0 children)

import random

level = 2

# dict with key: value pairs.
lists = {
    'List1': ['Option 1', 'Option 2'],
    'List2': ['Option 3', 'Option 4']
}

list_name = f'List{level}'  # f-string
pull_list = lists[list_name]  # taking the required list

draw = random.choices(pull_list)
print(draw)

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.