Breathe of fate
Breathe of fate

Reputation: 27

Convert input to variable

I have a code:

rock = ['lightning', 'gun', ...]
gun = ['lightning', 'sponge', ...]
lightning = ['wolf', 'sponge', ...]
devil = ['wolf', 'sponge', ...]
dragon = ['wolf', 'sponge', ...]
water = ['wolf', 'sponge', ...]
air = ['wolf', 'sponge', ...]
paper = ['wolf', 'sponge', ...]
sponge = ['wolf', 'rock', ...]
wolf = ['gun', 'rock', ...]
tree = ['gun', 'rock', ...]
human = ['gun', 'rock', ...]
snake = ['gun', 'rock', ...]
scissors = ['gun', 'rock', ...]
fire = ['lightning', 'gun', ...]

user_choice = input().split(",")

print(user_choice[0] + user_choice[1], sep=", ")

I'm trying to get a big list according to user input. So if user input "rock, gun", the list should look like this:

['lightning', 'gun', ..., 'lightning', 'sponge', ...]

User can input as many values as he wants. How can I make this?

Upvotes: 0

Views: 589

Answers (1)

game0ver
game0ver

Reputation: 1290

You could use a dictionary instead and do it like this:

d = {
    'scissors': ['gun', 'rock', ...],
    'fire': ['lightning', 'gun', ...]
}

choice0, choice1 = input().split(",")
print(d[choice0] + d[choice1])

If there will be more input values, an effective way to merge all sublists is using itertools module like so:

import itertools

# define dictionary etc...

user_choice = input().split(",")
lst = itertools.chain([d[choice] for choice in user_choice])
print(list(lst))

Upvotes: 2

Related Questions