我使用Ubuntu中的Terminal在.py文件(由Sublime Text v3126生成)中运行以下代码。 Ctrl + B在Sublime Text中不显示任何错误,但在终端中运行会产生错误。奇怪的是,这个代码在以前的.py文件中使用了Terminal,后者有其他代码。无论如何,这是特定的代码:
import math
import cmath
import datetime
import random as RAN
from tabulate import tabulate
def main():
print ("")
print("Key in a word.")
while True:
try:
x = str(input())
x = x.casefold()
xr = reversed(x)
print("")
print("Using iterative and/or recursive functions...")
except ValueError:
print("Invalid input. Try again")
print("")
continue
if (list(x) != list(xr)) or (x != x[::-1]):
print("The input is not a palindrome. Try again.")
print("")
continue
else:
print("The input is a palindrome.")
break
if __name__ == '__main__':
main()
因此,当我在仅包含此代码的.py文件中运行此代码时,我在终端中收到此错误:
Key in a word.
example
Traceback (most recent call last):
File "./Test Pad.py", line 30 in <module>
main()
File "./Test Pad.py", line 12 in main
x = str(input())
File "<string>", line 1, in <module>
NameError: name 'example' is not defined
感谢您的考虑。