Fanny Fanny - 23 days ago 7
Python Question

Python input() function

I am familiar with how the split() function works if you implement it like this:

def sayHello():
name = input("whats you´re name?:" )
print("hello", name)


In this case the input function only wants one input from user.
But what actually happens in this case?

def test():
str1, str2 = input().split()
print(str1, str2)


The syntax:

a, b = input()


Is this a way to ask the user for 2 inputs at the same time or when would you use this?

Answer

This is only working with a string of length 2.

Just try such things in ipython:

In [9]: a, b = input()
"hallo"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-3765097c12c0> in <module>()
----> 1 a, b = input()

ValueError: too many values to unpack

In [10]: a, b = input()
"ha"

In [11]: a
Out[11]: 'h'

In [12]: b
Out[12]: 'a'

In [13]: a, b = input()
"a"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-3765097c12c0> in <module>()
----> 1 a, b = input()

ValueError: need more than 1 value to unpack

So no, this not a correct way to ask for 2 inputs.

Comments