0

upon looking around a bit I have still not come to the conclusion as to why the following piece of python works in IDLE but not terminal. Any help would be appreciated.

print("Hello User!")

request_list = ['']

while True:
    greeting = input('')

    if greeting.lower() == "hello":
        print("Who is this?")
        print("Welcome back " + input() +", what can I do for you?")
        break

    elif greeting.lower() != "hello":
        print("Show some manners!")

The error

Traceback (most recent call last):
 File "courtney.py", line 23, in <module>
greeting = input('')
 File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
  • 1
    Your terminal is probably using Python 2, which is why when you use `input` it is throwing a `NameError`. Try running with `raw_input`, the Python 2 equivalent to Python 3 `input.` In Python 2, `input` is the equivalent to `eval(input())` in Python 3 – juanpa.arrivillaga Aug 17 '16 at 23:02
  • @juanpa.arrivillaga probably has the best guess. I just ran this in Python 3 and it appeared to work. Type `python --version` on the terminal and see if it prints 2.[something] instead of 3.[something] – Cody Aug 17 '16 at 23:05

2 Answers2

1

you are running python3 IDLE and the terminal is set to python2.

In your computer's environmental variables, you want to change the path to the location of your Python3 installation instead of the python 2.

Take a look at the picture, the one you want to change is PATH

Environmental Variables

If you dont want to change your environmental variables so that your terminal stays using python2, then you have to change your input and print statements.

the code below is the implementation of your code in python 2.7:

print "Hello User!" 

request_list = ['']

while True:
    greeting = raw_input("What is your name? ")

    if greeting.lower() == "hello":
        print "Who is this?"
        print "Welcome back " + greeting +", what can I do for you?"
        break

    elif greeting.lower() != "hello":
        print "Show some manners!"
lopezdp
  • 1,538
  • 3
  • 21
  • 38
1

The problem is that you use python 2.x in your terminal. If you have installed both you should be able to use the command 'python3' to run your code instead of the command 'python'.

In python 3 'input' can take an integer or a string. In python2 'input' cannot take a string. Only other stuff. In python 2.x you should use 'raw_input' take a string.

Tristan
  • 2,000
  • 17
  • 32
  • In fact, in python 2.x, input can take everything, even some python code (if my memory is working correctly). Which can lead to security issues. – romain-aga Aug 17 '16 at 23:29
  • Yea, you are partially right. It was more the idea of explaining the diffrence but I will change it. But it can't take everything (no strings) – Tristan Aug 17 '16 at 23:30
  • Well, input can take strings, if you use quotes x) – romain-aga Aug 17 '16 at 23:35
  • Didn't know that. :) – Tristan Aug 17 '16 at 23:36
  • I'm happy to make you learn something :) It just tedious to use input when you want a string, because raw_input is made for that, but also the really big point is that input can accept really anything. You will have to do some check to ensure you have the right type/value plus the security issue. It's why you don't have it in python 3 (if I don't say a mistake) – romain-aga Aug 17 '16 at 23:41