2

For example: (I know this is wrong, I just need to figure out the correct way.)

x, y, z = float(input('Enter what you would like x, y, and z to be.'))

So they would type 1 2 3 and each variable would be assigned in the corresponding order.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
A.j. Schenker
  • 23
  • 1
  • 4

1 Answers1

3

input() returns a single string, so you need to split it up:

>>> input('Enter what you would like x, y, and z to be: ').split()
Enter what you would like x, y, and z to be: 1.23 4.56 7.89
['1.23', '4.56', '7.89']

... and then convert each resulting string to a float:

>>> [float(s) for s in input('Enter what you would like x, y, and z to be: ').split()]
Enter what you would like x, y, and z to be: 9.87 6.54 3.21
[9.87, 6.54, 3.21]

... at which point you can assign the result to x, y and z:

>>> x, y, z = [float(s) for s in input('Enter what you would like x, y, and z to be: ').split()]
Enter what you would like x, y, and z to be: 1.47 2.58 3.69
>>> x
1.47
>>> y
2.58
>>> z
3.69

Of course, if the user inputs the wrong number of floats, you'll have a problem:

>>> x, y, z = [float(s) for s in input('Enter what you would like x, y, and z to be: ').split()]
Enter what you would like x, y, and z to be: 12.34 56.78
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)

... so it's probably a good idea to deal with it:

>>> while True:
...     try:
...         x, y, z = [float(s) for s in input('Enter what you would like x, y, and z to be: ').split()]
...     except ValueError:
...         print('Please enter THREE values!')
...     else:
...         break
... 
Enter what you would like x, y, and z to be: 1 2 3 4 5
Please enter THREE values!
Enter what you would like x, y, and z to be: 6 7
Please enter THREE values!
Enter what you would like x, y, and z to be: 8 9 0
>>> x
8.0
>>> y
9.0
>>> z
0.0
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • Thank you! This is exactly what i was looking for, much appreciated. And for my project it asks for length width and height so they have to enter 3 values. – A.j. Schenker Feb 26 '17 at 22:34