Python代码,从book&网站但仍然没有工作3.4

时间:2015-01-29 16:40:44

标签: python encryption

首先,我是Python的新手,很抱歉,如果这个问题很明显的话。检测英语模块似乎是错误的,但它在调用并独立运行时功能完全正常,单独运行时没有错误,并且我已经重写了几次以对其进行三重检查。

Traceback (most recent call last):
  File "H:\Python\Python Cipher Program\transposition hacker.py", line 49, in <module>
    main()
 File "H:\Python\Python Cipher Program\transposition hacker.py", line 11, in   main
 hackedMessage = hackTransposition(myMessage)
 File "H:\Python\Python Cipher Program\transposition hacker.py", line 34, in hackTransposition
  if detectEnglish.isEnglish(decryptedText):
File "H:\Python\Python Cipher Program\detectEnglish.py", line 48, in isEnglish
 wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
 TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

这是我在尝试运行转置黑客时遇到的错误(直接从here复制

以下是Detect English Module的代码        #检测英语模块

# to use this code
# import detectEnglish
# detectEnglish.isEnglish(somestring)
# returns true of false
# there must be a dictionary.txt file in the same directory
# all english words
# one per line

UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'


def loadDictionary()
 dictionaryFile = open('Dictionary.txt')
englishWords = {}
for word in dictionaryFile.read().split('\n'):
    englishWords[word] = None
dictionaryFile.close()
return englishWords

 ENGLISH_WORDS = loadDictionary()

def getEnglishCount(message):
message = message.upper()
message = removeNonLetters(message)
possibleWords = message.split()
if possibleWords == []:
    return 0.0

matches = 0
for word in possibleWords:
    if word in ENGLISH_WORDS:
        matches += 1
        return float(matches) / len(possibleWords)

def removeNonLetters(message):
lettersOnly = []
for symbol in message:
    if symbol in LETTERS_AND_SPACE:
        lettersOnly.append(symbol)
return ''.join(lettersOnly)

def isEnglish(message, wordPercentage=20, letterPercentage=85):
# by default 20% of the words mustr exist in dictionary file
# 85% of charecters in messafe must be spaces or letters
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
numLetters = len(removeNonLetters(message))
messageLettersPercentage = float(numLetters) / len(message) * 100
lettersMatch = messageLettersPercentage >= letterPercentage
return wordsMatch and lettersMatcht

2 个答案:

答案 0 :(得分:2)

getEnglishCount看起来缺少一个return语句。如果python在没有命中return语句的情况下到达函数的末尾,它将返回None,因为你正在看到。

试试这个:

def getEnglishCount(message):
    message = message.upper()
    message = removeNonLetters(message)
    possibleWords = message.split()
    # if possibleWords == []:  # redundant
    #     return 0.0
    return len(possibleWords)

编辑: @Kevin是的,我认为你是对的 - 那个功能还有更多。也许试试这个:

def getEnglishCount(message):
    message = message.upper()
    message = removeNonLetters(message)
    possibleWords = message.split()
    if possibleWords == []:
        return 0.0

    matches = 0.
    for word in possibleWords:
        if word in ENGLISH_WORDS:
            matches += 1
    return matches / len(possibleWords)

当您复制并粘贴代码时,我猜测缩进以某种方式被更改了,并且return语句嵌套在if下。

答案 1 :(得分:0)

正如另一张海报所说,你错过了getEnglishCount方法的回报,所以它返回NoneType,这意味着没有要返回的值。

您无法对NoneTypes进行数学运算,因此NoneType * 100会失败,这就是错误回溯的底部所说的。