python - 获取 Traceback(最近一次调用最后一次):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()
得到
Traceback (most recent call last):
main()
NameError: name 'main' is not defined
有人会帮我运行这段代码吗?已经尝试了一切都没有工作无法运行它显示此错误的程序?
解决方案
在调试此类问题时,它有助于删除所有不相关的内容并找到仍然产生相同错误的最小程序。由于程序几乎立即崩溃,几乎一切都可以进行。此脚本产生相同的错误
class Cipher(object):
def main():
print("not fubar")
if __name__ == '__main__':
main()
问题很容易发现:main
不应该是课堂的一部分。
class Cipher(object):
pass
def main():
print("not fubar")
if __name__ == '__main__':
main()
将该修复应用于实际程序,您将进入下一个问题。
推荐阅读
- javascript - 从组件化的 ReactiveFrom 输入发送验证到另一个 ReactiveForm
- python - 根据现有日期列中的年份添加二进制分类列
- r - R Shiny:测试 req() 之后可选的反应值是否存在
- list - 如何将 JSON 列表中的每个元素相互组合
- json - 如何将数组编码为 JSON 字段的值?
- python - 给定一个列表列表,如何在 Python 中创建一个包含所有可能值组合的矩阵?
- java - MediaSessionCompat:Targeting S+ (version 31 and above) 要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一
- reactjs - 在 react js 中使用 filereader 读取多个文件
- scala - 如何通过手动调用单独测试回调?
- json - 使用 curl 将 json 文件摄取到 ElasticSearch