简体   繁体   中英

How to take 2 user inputs and store them in a variable in Python?

I need to take 2 user inputs and turn them into a list and further into variables.

I have a starting command which I can ignore by splitting the message with

user_input = str(message.content).split('!command ')

but the outcome is ['', 'input1, input2'] and I need the whole thing to be seperated like ['', 'input1', 'input2'] so I can take the inputs with indexing and further work with them.

Try:

user_input = str(message.content).split('!command ')
user_input = [y for x in user_input for y in x.split(', ')]

If you want to assign them to separate variables in one line:

input1, input2 = str(message.content).split('!command ')[1].split(', ')

Try this ( it's a bit dirty but it works):

data = '!command input1, input2'
inputs = data.split('!command ')[-1].split(', ') #to get inputs

But why there is a comma between input 1 and 2? Is it better if the command is:

'!command input1 input2'

You can split by space and then get inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM