原文网址: https://fishc.com.cn/thread-201013-1-1.html
以下为原文粘贴:def morsecode2(code):
# 摩斯密码对比表
c_table = [".-", "A", "-...", "B", "-.-.", "C", "-..", "D",
".", "E", "..-.", "F", "--.", "G", "....", "H",
"..", "I", ".---", "J", "-.-", "K", ".-..", "L",
"--", "M", "-.", "N", "---", "O", ".--.", "P",
"--.-", "Q", ".-.", "R", "...", "S", "-", "T",
"..-", "U", "...-", "V", ".--", "W", "-..-", "X",
"-.--", "Y", "--..", "Z", ".----", "1", "..---", "2",
"...--", "3", "....-", "4", ".....", "5", "-....", "6",
"--...", "7", "---..", "8", "----.", "9", "-----", "0"]
split_code = code.split(" ")
result = [c_table[c_table.index(each) + 1] for each in split_code]
return result
import timeit
timeit.timeit("morsecode2(test_code)", setup="from __main__ import morsecode2, test_code")
使用的python版本:3.11.1
报错如下:
timeit.timeit("morsecode2(test_code)", setup="from __main__ import morsecode2, test_code")
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
timeit.timeit("morsecode2(test_code)", setup="from __main__ import morsecode2, test_code")
File "C:\Users\2023x's\AppData\Local\Programs\Python\Python311\Lib\timeit.py", line 234, in timeit
return Timer(stmt, setup, timer, globals).timeit(number)
File "C:\Users\2023x's\AppData\Local\Programs\Python\Python311\Lib\timeit.py", line 178, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 3, in inner
ImportError: cannot import name 'test_code' from '__main__' (unknown location)
|