Python Forum
User input question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: User input question (/thread-11462.html)



User input question - linuxnoob - Jul-10-2018

Bare with me, coding is completely out of my wheelhouse! I'm working through a book called "Python Crash Course". I'm trying to work with a really simple program that should accept user input. It isn't working in the text editor (Sublime Text)which makes sense I guess. But when I try to run the code from the command line I get the error below. This confuses me because obviously the program is getting my input. I apologize if its something really simple, I'm really new to Python. I should also mention that I can get the program to work with Idle but I'd really like to continue using Sublime Text if possible.

name = input("Please enter your name: ")
print("Hello, " + name + "!")
Output:
Please enter your name: Chris
Error:
Traceback (most recent call last): File "greeter.py", line 2, in <module> name = input("Please enter your name: ") File "<string>", line 1, in <module> NameError: name 'Chris' is not defined



RE: User input question - buran - Jul-10-2018

Please, read
https://python-forum.io/Thread-Basic-Python3-2-differences-input-vs-raw-input

By the way, it's bit confusing that you claim it works in IDLE. Do you have more than one python installations?


RE: User input question - linuxnoob - Jul-10-2018

(Jul-10-2018, 03:57 AM)buran Wrote: Please, read
https://python-forum.io/Thread-Basic-Python3-2-differences-input-vs-raw-input

By the way, it's bit confusing that you claim it works in IDLE. Do you have more than one python installations?

After reading your link I'm going to check to verify what version of Python I'm using. The program does work when I run it in IDLE, I don't think I have more than one installation but I'll double check.


RE: User input question - linuxnoob - Jul-11-2018

Mystery solved...When trying to run the program from the command line in Mac I was entering:
python <file name> when I should have been entering python3 <file name>. Changing that command fixed it for me.

However, when I enter python --version from my terminal I get back 2.7.10 even though I just upgraded to 3.7.0...


RE: User input question - ichabod801 - Jul-11-2018

(Jul-11-2018, 03:43 AM)linuxnoob Wrote: However, when I enter python --version from my terminal I get back 2.7.10 even though I just upgraded to 3.7.0...

Try python3 --version.


RE: User input question - buran - Jul-11-2018

You have 2 python installations. On Mac it comes with python2 pre-installed. With python you invoke python2, with python3, well you invoke python3


RE: User input question - snippsat - Jul-11-2018

(Jul-11-2018, 03:43 AM)linuxnoob Wrote: Mac I was entering:
Doing it Right Mac

Alternative pyenv(Simple Python Version Management) also work on Mac.
Example how it look on Linux:
# Update pyenv
mint@mint ~ $ pyenv update
 
# Look at what's available
mint@mint ~ $ pyenv install --list
Available versions:
....
3.6.6
  3.6.6rc1
  3.7.0
  3.7-dev
  3.8-dev
....
 
# Update needed
sudo apt-get update
sudo apt-get install libffi-dev
 
# install
mint@mint ~ $ pyenv install 3.7.0
Installing Python-3.7.0...
Installed Python-3.7.0 to /home/mint/.pyenv/versions/3.7.0
 
# Set python and pip to point to 3.7
mint@mint ~ $ pyenv global 3.7.0
mint@mint ~ $ python -V
Python 3.7.0
mint@mint ~ $ pip -V
pip 10.0.1 from /home/mint/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pip (python 3.7)
mint@mint ~ $ 
# Finish
So now python and pip always point to what you set global in this case 3.7.


RE: User input question - linuxnoob - Jul-12-2018

(Jul-11-2018, 05:27 AM)buran Wrote: You have 2 python installations. On Mac it comes with python2 pre-installed. With python you invoke python2, with python3, well you invoke python3

Well now I feel a bit dumb! Should I remove python2?


RE: User input question - buran - Jul-12-2018

No, it's system Python and is required.
Read Doing it right Mac link from snippsat's post #7


RE: User input question - linuxnoob - Jul-12-2018

(Jul-12-2018, 04:03 AM)buran Wrote: No, it's system Python and is required.
Read Doing it right Mac link from snippsat's post #7

I installed Python 3 before seeing this post unfortunately. Hopefully it will work well enough for me.