所以,如果我想接受来自用空格分隔的用户的输入,我会使用这段代码:
x, _, x2 = input("> ").lower().partition(' ')
哪个工作正常。但是,如果我想接受3个响应,那么我会得到一个ValueError:
x, _, x2, _, x3 = input("> ").lower().partition(' ')
ValueError: not enough values to unpack (expected 5, got 3)
那么,我如何使用这种(或另一种)方法接受两个以上的“输入”呢?
答案 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]