简体   繁体   English

获取回溯(最近一次调用最后一次):main() NameError:未定义名称“main”

[英]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.问题很容易发现: main不应该是类的一部分。

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.将该修复程序应用于实际程序,然后您将进入下一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 追溯(最近一次通话最后一次):NameError:未定义名称“路径” - Traceback (most recent call last):NameError: name 'path' is not defined 获取回溯(最近一次调用最后一次):文件“<stdin> &quot;,第 1 行,在<module> NameError:名称“文件名”未定义 - Getting Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'filename' is not defined Traceback(最近一次调用最后一次):,第 131 行,在<module> print(self.marka) NameError: name 'self' is not defined</module> - Traceback (most recent call last): , line 131, in <module> print(self.marka) NameError: name 'self' is not defined 回溯(最近调用最后):文件“<stdin> “,第 1 行,在<module> NameError:名称“游泳”未定义</module></stdin> - Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'swim' is not defined 在 AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback(最近一次调用最后)中运行的 Py 脚本: - Py Script running in AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback (most recent call last): 回溯(最近调用最后):文件“<stdin> ",第 1 行,在<module> NameError: 名称 'db' 未定义</module></stdin> - Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'db' is not defined 无法在python 3Traceback(最近一次调用为最新)中找出此错误:“文件” <stdin> ”,第1行,在 <module> NameError:未定义名称“ xlrd” - Cannot figure out this error in python 3Traceback (most recent call last): File “<stdin>”, line 1, in <module> NameError: name 'xlrd' is not defined 我正在获取 Traceback Traceback(最近一次通话最后一次): - i am getting Traceback Traceback (most recent call last): 回溯(最近一次调用最后一次):文件“main.py”,第 11 行,在<module> - Traceback (most recent call last): File "main.py", line 11, in <module> 在线程 django-main-thread 中使用 StatReloader Exception 监视文件更改:Traceback(最近一次调用最后一次): - Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM