0

Input function is only accepting integers as an input otherwise I receive this error message when I run using REPL in python 3:

Entry:
b
Traceback (most recent call last):
  File "Ex7.5.py", line 1, in <module>
    a = input("Entry:\n")
  File "<string>", line 1, in <module>
NameError: name 'b' is not defined

Need my code to accept both letters and numbers as input and cant understand why it's not taking b as a string and printing it?

Literally just trying to get this print input function to work currently to then use later in other functions.

If I run the same code with integers only it works no problem.

a = input("Entry:\n")
print(a)
print(type(a))

The answer I'm expecting is b.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Doesn't seem to have anything wrong with your code, it has your expected behavior when I run it here. Which version of python are you using? You can see that with command `python --version`. – Telmo Trooper Aug 30 '19 at 19:59
  • I'm using python 3 in Sublime but if I enter it into the Terminal (mac) I get python 2.7? raw_input I tried also but also didn't work (imagine because Sublime is running Python 3) – Luke Hansford Aug 30 '19 at 20:02
  • Does it work if you run it as `python3 Ex7.5.py`? – Telmo Trooper Aug 30 '19 at 20:02
  • /Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'Ex7.5.py': [Errno 2] No such file or directory is the error message? – Luke Hansford Aug 30 '19 at 20:04
  • Can you `cd PATH_TO_FOLDER` and then run that command and see if it works? The error you showed me says there's not `Ex7.5.py` file in the directory where you ran the command. – Telmo Trooper Aug 30 '19 at 20:05
  • 1
    So the error you describe in the question has to do with the fact that you're running your code in Python 2 instead of Python 3. Your other exception in the comments is about not running the program correctly when you try to use Python 3 explicitly. – Blckknght Aug 30 '19 at 20:06
  • have you tried just saving this and running the python code outside of sublime. Maybe sublime is your issue rather then python. As others says your code works – Chris Doyle Aug 30 '19 at 20:08
  • The code works outside of Sublime yes, thankyou. I would really like to fix why it's not working in Sublime however! When trying to cd PATH_TO_FOLDER it gives "No such file or directory". I think its maybe to do with where the saved file is? Or maybe I have an environment set up by accident? Any advice? – Luke Hansford Aug 30 '19 at 20:14
  • Possible duplicate of [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined), including, oddly enough, the part where Python 2 is unexpectedly in use. – Davis Herring Aug 31 '19 at 00:40

1 Answers1

1

This error is occurring because you're running the code in Python 2, not Python 3. input() in Python 2 will evaluate what you give it, in this case as a variable name, which doesn't exist; while input() in Python 3 will keep it as a string. For more details see What's the difference between raw_input() and input() in python3.x?

How to use the correct Python version is another question, but it looks like you're making some progress in the comments so far.

wjandrea
  • 28,235
  • 9
  • 60
  • 81