最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python檢查字符串是否是正確ISBN的方法

 更新時(shí)間:2015年07月11日 11:52:47   作者:pythoner  
這篇文章主要介紹了python檢查字符串是否是正確ISBN的方法,涉及Python針對字符串的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python檢查字符串是否是正確ISBN的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

def isISBN(isbn): 
  """Checks if the passed string is a valid ISBN number.""" 
  if len(isbn) != 10 or not isbn[:9].isdigit(): 
    return False 
  if not (isbn[9].isdigit() or isbn[9].lower() == "x"): 
    return False 
  tot = sum((10 - i) * int(c) for i, c in enumerate(isbn[:-1])) 
  checksum = (11 - tot % 11) % 11 
  if isbn[9] == 'X' or isbn[9] == 'x': 
    return checksum == 10 
  else: 
    return checksum == int(isbn[9]) 
ok = """031234161X 0525949488 076360013X 0671027360 0803612079 
    0307263118 0684856093 0767916565 0071392319 1400032806 0765305240""" 
for code in ok.split(): 
  assert isISBN(code) 
bad = """0312341613 052594948X 0763600138 0671027364 080361207X 0307263110 
     0684856092 0767916567 0071392318 1400032801 0765305241 031234161 
     076530Y241 068485609Y""" 
for code in bad.split(): 
  assert not isISBN(code) 
print "Tests of isISBN()passed." 

希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

宁强县| 慈溪市| 永修县| 武强县| 贵溪市| 龙陵县| 西充县| 江川县| 项城市| 耒阳市| 巨野县| 金湖县| 三河市| 舟曲县| 同德县| 类乌齐县| 阿合奇县| 黔西| 吉木乃县| 宾川县| 柳州市| 都昌县| 巍山| 台山市| 大庆市| 旬邑县| 界首市| 曲沃县| 年辖:市辖区| 徐州市| 格尔木市| 永胜县| 桐庐县| 新宾| 阳山县| 花莲县| 张家港市| 会宁县| 饶平县| 天津市| 海林市|