尝试在输入中使用3个以上的值时的ValueError

时间:2017-01-13 01:22:11

标签: python python-3.x

所以,如果我想接受来自用空格分隔的用户的输入,我会使用这段代码:

x, _, x2 = input("> ").lower().partition(' ')

哪个工作正常。但是,如果我想接受3个响应,那么我会得到一个ValueError:

x, _, x2, _, x3 = input("> ").lower().partition(' ')
ValueError: not enough values to unpack (expected 5, got 3)

那么,我如何使用这种(或另一种)方法接受两个以上的“输入”呢?

1 个答案:

答案 0 :(得分:1)

partition方法始终只返回3个值:

S.partition(sep) -> (head, sep, tail)

Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it.  If the separator is not
found, return S and two empty strings.

您可能想要split

x1, x2, x2 = input("> ").lower().split(' ')

或更灵活:

xs = input("> ").lower().split(' ')

或者一次性拍摄:

x1, x2, x3 = (input("> ").lower().split(' ') + [None, None, None])[0:3]