简体   繁体   中英

getting Traceback (most recent call last): main() NameError: name 'main' is not defined

from letprob import *

class Cipher(object):
    def __init__(self, inputString):
        self.inputString = inputString
        self.encodedString = ''
        self.decodedString = ''

    def __repr__(self):
        s = 'Original String: %s\nEncoded String: %s\nDecoded String: %s' \
        % (self.inputString, self.encodedString, self.decodedString)
        return s
  
    def encipher(self, n):

        for i in (self.inputString):


            if 'a' <= i <= 'z':
                encWord = (ord(i) + n - 97) % 26 + 97
                encWord1 = chr(encWord)
                self.encodedString += encWord1


            elif 'A' <= i <= 'Z':
                newWord = (ord(i) + n - 65) % 26 + 65
                newWord1 = chr(newWord)
                self.encodedString += newWord1

            else:
                 self.encodedString += i
  

    def decipherEasy(self, n):

        for i in (self.encodedString):


            if 'a' <= i <= 'z':
                decWord = (ord(i) - n - 97) % 26 + 97
                decWord1 = chr(decWord)
                self.decodedString += decWord1


            elif 'A' <= i <= 'Z':
                newWordD = (ord(i) - n - 65) % 26 + 65
                newWord1D = chr(newWordD)
                self.decodedString += newWord1D

            else:
                self.decodedString += i




    def shift(self, encodedString, n):
        decodedString = ''
        for i in (encodedString):


            if 'a' <= i <= 'z':
                decWord = (ord(i) + n - 97) % 26 + 97
                decWord1 = chr(decWord)
                decodedString += decWord1


            elif 'A' <= i <= 'Z':
                newWordD = (ord(i) + n - 65) % 26 + 65
                newWord1D = chr(newWordD)
                decodedString += newWord1D

            else:
                decodedString += i
                return decodedString


    def decipher(self):
        shiftList = []
        score = 0

        for i in range(0, 26+1):
            shiftStr = self.shift(self.encodedString, i)
            prob = self.possibleProb(shiftStr)
            shiftList.append([shiftStr, prob])

            self.sortProb(shiftList)

#print(shiftList)
#return shiftList


    def sortProb(self, List):
        size = len(self.shiftList)

        for i in range(size):
            for j in range(1, size):
                if List[j][1] > List[j - 1][1]:
                    print(j)
                    temp = List[j]
                    List[j] = List[j - 1]
                    List[j-1] = temp

                else:
                    return List
  

    def possibleProb(self, sentence):

        s = 0

        for i in sentence:
            s += letProb(i)
            return s


    def main():
        cipher = Cipher('this')
        cipher.encipher(1)
# cipher.decipherEasy(1)
        cipher.decipher()
        print(cipher)



if __name__ == '__main__':
    main()

getting

Traceback (most recent call last):
  
    main()
NameError: name 'main' is not defined

Will someone help me functioning this code? have tried everything nothing worked unable to run the program it shows this error?

When debugging this type of problem, it helps to remove everything that is not related and get down to the smallest program that still produces the same error. Since the program crashes almost immediately, almost everything can go. This script produces the same error

class Cipher(object):
    def main():
        print("not fubar")

if __name__ == '__main__':
    main()

And the problem is easy to spot: main should not be part of the class.

class Cipher(object):
    pass

def main():
    print("not fubar")

if __name__ == '__main__':
    main()

Apply that fix to the real program and you are on to the next issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM