!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!

commented: Good stuff. Thanks. +1
commented: Good. +3
commented: nice +2

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

nice, it looks like a kind of 'scanf' for python.

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 :(.

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

Wow a tutorials, i havent seen one of these posted for aages...

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...

Thank you for this tip.

commented: Thanks not by post, by upvote, please! -3

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.

Member Avatar

111100/11000

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
Member Avatar

111100/11000

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