Skip to main content
 首页 » 编程设计

Python:检查数组是否没有所需数量的成员

2024年10月01日6youxin

我有一个客户端-服务器项目,其中我有一个方法 customs,它在购买汽车时计算海关付款。它有两个形式参数:YearofProduction 和 CostOfCar。

在客户端命令行中,我输入

CUSTOMS 2017 20000

并得到X笔钱的返回。让我们调用 CUSTOMS 2017 20000 theRequest(所以它是一个字符串)。

服务器中的Python代码:

data = conn.recv(1024) 
point = data.decode("ASCII") 
points = point.split(' ') 
MESSAGE = points[0] 

并调用它:

if points[0]=="CUSTOMS": 
    if points[1].isdigit(): #the second argument is float, so .isdigit() is not working 
        CUSTOMS(points[1], points[2]) 
    else: 
        MESSAGE = "Input error" 
        conn.send(MESSAGE.encode("ASCII")) 
else: 
     MESSAGE = "Input Error" 
     conn.send(MESSAGE.encode("ASCII"))  

问题是当我不输入第二个参数时,服务器不响应任何内容。我怎么能说 if points[2] == null(由于动态向量,points[2] 不存在,情况不可能如此)。

或者,我怎样才能让它检查向量有多少个成员?如果 2(消息(points[0])和第一个参数(points[1]),则显示错误消息?

请您参考如下方法:

使用 len 内置函数获取任何有限集合(列表、元组、集合...)的长度。

(或者更准确地说,任何是 Sized 对象,即具有__len__ 方法。)