!USING PYTHON 3.1!

Hello DaniWeb! Today I'm going to teach you a cool variable trick that I learned. So here's an example:

a, b, c = input('?x?x?: ').split('x')

Now let's input something like:

>>> a, b, c = input('?x?x?: ').split('x')
?x?x?: 1x2x3

Now, variable 'a' is set to 1, variable 'b' is set to 2, variable 'c' is set to 3. I hope I explained that well enough. It can be used in various sections. NOTE: Because you used 3 variables, you have to have three answers divided by the character you split with. If you only used 'a, b' you would only be able to do '1x2'.

Now lets use another example using more common names:

breakfast, lunch, dinner = input('breakfast&lunch&dinner').split('&')

Now if we run this in idle the result is:

>>> breakfast, lunch, dinner = input('breakfast&lunch&dinner: ').split('&')
breakfast&lunch&dinner: sausage&pizza&chicken

Now:

breakfast = sausage
lunch = pizza
dinner = chicken

That's going to be it for this tutorial! Hope you find it useful!

Edited 7 Years Ago by AutoPython: n/a

Comments
Good stuff. Thanks.
Good.
nice

You should use this with a try/except (within is while) since anyone would enter more or fewer characters at some time.

Thanks for the feed back. I would edit it and show an example with the try/except thing, but I've already used all my edits :(.

Edited 7 Years Ago by AutoPython: n/a

That's a problem with these code snippet. In the active state snippets, people can update their code long after the initial post.

Yeah, I thought the same thing. I want to post more tutorials here. It's just that I don't have to many good subjects to post on.

This is good, it will really reduce the number of input() functions i use in my programs.
Thanks...

Coincidentally how to do a multi-input was rattling around my head for the past two weeks. Tonight, I click on turtorials for S & G, and bam!, there's the answer.

Thanks.

awesome
but shouldn't that be reversed

breakfast&lunch&dinner: sausage&pizza&chicken

breakfast&lunch&dinner: sausage&chicken&pizza

than

breakfast = sausage
lunch = chicken
dinner = pizza

you can also do:

x,y,z = input('Enter 3 numbers: ')

than if you enter:

Enter 3 numbers: 567
x=5
y=6
z=7