查看: 1911|回复: 0

Python NameError: name is not defined (related to having default input argume...

[复制链接]

665

主题

1234

帖子

6671

积分

xdtech

Rank: 5Rank: 5

积分
6671
发表于 2019-1-22 11:58:48 | 显示全部楼层 |阅读模式
本帖最后由 shaoheshaohe 于 2019-1-22 12:03 编辑

def circularFind(myByteArray, searchVal, start=0, end=len(myByteArray)):   
"""    Return the first-encountered index in bytearray where searchVal     is found, searching to the right, in incrementing-index order, and    wrapping over the top and back to the beginning if index end <     index start    """   
if (end >= start):        
     return myByteArray.find(searchVal, start, end)   
else:
#end < start, so search to highest index in bytearray, and then wrap around and search to "end" if nothing was found         
     index
= myByteArray.find(searchVal, start, len(myByteArray))
        
if (index == -1):            
#if searchVal not found yet, wrap around and keep searching            
     index
= myByteArray.find(searchVal, 0, end)        
return index             
      
        
错误:
NameError:未定义名称'myByteArray'
但是,如果我只删除我的默认参数(= 0和= len(myByteArray)),它可以正常工作。但是......我真的想要那些默认参数,以便start和end参数是可选的。我该怎么办?


修改:

def circularFind(myByteArray, searchVal, start=0, end=None):
    """    Return the first-encountered index in bytearray where searchVal     is found, searching to the right, in incrementing-index order, and    wrapping over the top and back to the beginning if index end <     index start    """   
if end is None:        
     end
= len(myByteArray)   
# continue doing what you were doing
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表