是否有可能在python中修复列表的大小,以便如果列表包含更多,它将引发错误

问题描述 投票:0回答:2
n = int(input()) #fixed length
l = input().split()

我想要的列表应该作为一行输入给出,所以我不知道范围将如何帮助?

python python-3.x
2个回答
1
投票

使用len()检查长度并引发错误。

if len(l)>n:
    raise Exception('Please give only {} inputs'.format(n))

1
投票

您可以使用len来确定列表或元组的大小。

if len(l) > n: # assuming n is the max size 
    print("List too large")
© www.soinside.com 2019 - 2024. All rights reserved.