1

What's wrong with this bit of Python? Entering the names does not work.

def main():
    print("This program generates computer usernames.\n")

    # get user's first and last names
    first = input("Please enter your first name (all lowercase): ")
    last = input("Please enter your last name (all lowercase): ")

    # concatenate first initial with 7 chars of the last name
    uname = first[0] + last[:7]

    # output the username
    print("Your username is:", uname)

main()

Now, running the program results in this -- no idea what it's all about.

cd '/Users/ek/Desktop/' && '/usr/bin/pythonw'                     '/Users/ek/Desktop/fun.py'  && echo Exit status: $? && exit 1
EKs-Mac-mini:~ ek$ cd '/Users/ek/Desktop/' && '/usr/bin/pythonw'  '/Users/ek/Desktop/fun.py'  && echo Exit status: $? && exit 1


This program generates computer usernames.

Please enter your first name (all lowercase): Bob


Traceback (most recent call last):
File "/Users/ek/Desktop/fun.py", line 14, in <module>
main()
File "/Users/ek/Desktop/fun.py", line 5, in main
first = input("Please enter your first name (all lowercase): ")
File "<string>", line 1, in <module>
NameError: name 'Bob' is not defined
EKs-Mac-mini:Desktop ek$ 
Edmund
  • 17
  • 4

2 Answers2

0

In your code, change input (which works in Python 3) to raw_input (which works in Python 2) and see what happens.

And to confirm which Python version you're using, run this Terminal command: python --version

jkdev
  • 9,037
  • 14
  • 52
  • 75
  • Ehhh, I'm using Python 3. Thanks though. – Edmund Jun 29 '16 at 00:27
  • Crap, no, when I kick off the program it's using Python 2 on the terminal. Any idea how to get the terminal to use Python 3? – Edmund Jun 29 '16 at 16:19
  • Try running `python3` instead of `python`. – jkdev Jun 29 '16 at 16:22
  • Forgive my ignorance, but if I have the program sitting there, how do I get it to run on python3 instead of python when I just double click on it? How do I get the terminal to automatically do so? – Edmund Jun 30 '16 at 19:39
  • What OS are you using? (Linux, Mac, Windows...) – jkdev Jun 30 '16 at 19:42
  • I would try searching SO or Google for "Change default Python", "Switch default Python", or similar search terms. – jkdev Jun 30 '16 at 19:46
  • OK. What specific program are you running as your Python environment? (The one you double-click on to open.) – jkdev Jun 30 '16 at 23:10
  • The one originally described in my post. Is there any command I can add at the start of a Python program to ensure that a specific version of Python is used? def main(): print("This program generates computer usernames.\n") # get user's first and last names first = input("Please enter your first name (all lowercase): ") last = input("Please enter your last name (all lowercase): ") # concatenate first initial with 7 chars of the last name uname = first[0] + last[:7] # output the username print("Your username is:", uname) main() – Edmund Jul 01 '16 at 01:31
  • I think here's the answer: In the first line of the Python script, [use a shebang (`#!`) followed by the absolute filepath to the version of Python you want to run](http://stackoverflow.com/a/21670658/3345375). – jkdev Jul 01 '16 at 02:47
  • And on the flip side, check out this question: [How do I check what version of Python is running my script?](http://stackoverflow.com/q/1093322/3345375) – jkdev Jul 01 '16 at 02:49
  • One more thing: To find the path to version 3 of Python (to see where Python 3 is installed on your computer), run this Terminal command: `which python3` – jkdev Jul 01 '16 at 03:31
  • I tried different permutations of the shebang, but it did not seem to work. My Python 3.5 is installed under the rest of the apps, not like the Python 2.7 that came built in. – Edmund Jul 02 '16 at 01:20
  • Hey jkdev, do you have any idea what the correct path should be if Python 3 is installed under apps? Or should I move it somewhere else for the #! to work? Pardon my ignorance, I'm a neophyte. – Edmund Jul 02 '16 at 19:09
  • Try running `which python3` in Terminal. – jkdev Jul 03 '16 at 04:07
  • /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 I also checked the .bash_profile file and here is the version of python set: # Setting PATH for Python 3.5 # The original version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}" export PATH – Edmund Jul 03 '16 at 17:37
  • But when I open terminal and type "python --version" I get Python 2.7.10 – Edmund Jul 03 '16 at 17:37
  • How about when you type `python3 --version`? – jkdev Jul 03 '16 at 17:40
  • (You'd probably get `Python 3.5`. Just making sure.) – jkdev Jul 03 '16 at 20:59
  • I've posted [another answer](http://stackoverflow.com/a/38174826/3345375) based on these comments. – jkdev Jul 04 '16 at 02:32
0

Instead of changing input to raw_input so your script will work in Python 2, you could add a shebang line so the Terminal shell will run your script in Python 3.

A shebang line tells the shell (1) that the file is a shell script, (2) which Python interpreter to use, and (3) where to find that interpreter (the file path).

Then you should just be able to run $ fun.py in Terminal. You shouldn't need to specify $ python3 fun.py.


Two ways to write the shebang line.

1) Put this line at the top of your fun.py file:

#! /usr/bin/env python3

That is the recommended way to write the shebang line. It works in UNIX-like operating systems (including Linux, Mac OS X, etc.)

2) Or, specify the complete absolute path to the location where Python 3 is installed. In your case:

#! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3

If you don't know the absolute path, find it using the Terminal command $ which python3.


P.S. As an alternative, changing the default version of Python on your system from Python 2 to Python 3 might also work. But it has other bad consequences so don't do it.

Community
  • 1
  • 1
jkdev
  • 9,037
  • 14
  • 52
  • 75
  • (jibber jabber) This program generates computer usernames. Please enter your first name (all lowercase): Ed Traceback (most recent call last): File "/Users/ek/Desktop/#! :usr:bin:env python3.py", line 16, in = main() File "/Users/ek/Desktop/#! :usr:bin:env python3.py", line 7, in main first = input("Please enter your first name (all lowercase): ") File "", line 1, in NameError: name 'Ed' is not defined EKs-MacBook-Pro:Desktop ek$ – Edmund Jul 04 '16 at 18:10
  • This happens with both versions of the shebang – Edmund Jul 04 '16 at 18:12
  • Put some code in the script to find out what version of Python is running the script: http://stackoverflow.com/a/1093331/3345375 – jkdev Jul 04 '16 at 18:16
  • `import sys` and then `print(sys.version)` – jkdev Jul 04 '16 at 18:17
  • Gotcha -- here's what we get: Last login: Mon Jul 4 17:09:11 on ttys000 cd '/Users/ek/Desktop/' && '/usr/bin/pythonw' '/Users/ek/Desktop/username.py' && echo Exit status: $? && exit 1 EKs-Mac-mini:~ ek$ cd '/Users/ek/Desktop/' && '/usr/bin/pythonw' '/Users/ek/Desktop/username.py' && echo Exit status: $? && exit 1 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] Exit status: 0 logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed] – Edmund Jul 04 '16 at 21:09
  • Correction -- here it is with shebang: Last login: Mon Jul 4 17:13:28 on ttys000 EKs-Mac-mini:~ ek$ cd '/Users/ek/Desktop/' && '/Library/Frameworks/Python.framework/Versions/3.5/bin/python3' '/Users/ek/Desktop/username.py' && echo Exit status: $? && exit 1 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] Exit status: 0 logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed] – Edmund Jul 04 '16 at 21:14
  • 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] – jkdev Jul 05 '16 at 00:05
  • So it's version 3.5.2. The `input` function should work in that version. I wish I knew why it doesn't... – jkdev Jul 05 '16 at 00:06
  • At least we know that the shebang is working. Without it, the script is run with Python 2.7.10: `2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]`. With the shebang, the script is run with Python 3.5.2: `3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]` – jkdev Jul 05 '16 at 00:09
  • I think what you should do is [ask a new Stack Overflow question](https://stackoverflow.com/questions/ask): Post the Python code, including the shebang line, `import sys` and `print(sys.version)`; and also post the messages you're getting in Terminal. Then ask why the script seems to be running in Python 2 instead of Python 3. – jkdev Jul 05 '16 at 01:38
  • Will do -- I noticed it was running version 3 as well from the output! Thanks for all your help man, I'm upvoting you anyway before I start the new one. – Edmund Jul 05 '16 at 14:51
  • Wish we could friend or follow people -- I really appreciate your going over this. – Edmund Jul 05 '16 at 14:52