0

I ran the following code in a .py file (generated by Sublime Text v3126) using Terminal in Ubuntu. Ctrl+B doesn't show any errors in Sublime Text but running in Terminal yields an error. Strangely, this code worked in Terminal when it was in a previous .py file, which had other codes. Anyway, here's the particular code:

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()

So, when I run this code in a .py file containing only this code, I get this error in Terminal:

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 

Thank you for your consideration.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stoverflow
  • 65
  • 6
  • The code breaks because you are running it with Python 2, not Python 3. – Martijn Pieters May 06 '18 at 12:32
  • Thanks for the quick response. However, when I Import sys and print(sys.version_info) I get: sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0) This means it's the latest interpreter, right? – Stoverflow May 06 '18 at 12:56
  • It means you are running Python 2. 2.7.12, to be exact. There are material differences between Python 2 and Python 3, one of which is what exactly the `input()` function does. Python 3 is up to 3.6.5 currently, and the Python 3.7.x series is in beta. – Martijn Pieters May 06 '18 at 13:08
  • I just ran the older file I described earlier, the one that wasn't supposed to break but now it breaks too when trying to input strings. I suspect it has something to do with my trainer going around doing some re-installing via Terminal that day when many of us trainees couldn't run codes with the Super() function. Prior, the .py files ran fine for me. The Python version must have changed along the way. I'll have to check with the trainer next time. Btw, thx for showing me how to read the Python version, I thought releaselevel=final simply meant the latest version but now I know better! – Stoverflow May 06 '18 at 13:35

0 Answers0