-2

So im trying to make a conversation generator in python using the fashion of an AI where the user will say something like 'hi' and then the machine replies 'hello how are you' and as you can guess, this will need alot of user input but every time i input ANYTHING, i get the output:

 Traceback (most recent call last):
  File "main.py", line 4, in <module>
    talk=input('')
  File "<string>", line 1, in <module>
NameError: name '___' is not defined

heres my code:

#Modules
import os
#Getting User Input
talk=input('')
#Machine responding
if talk == "Hi":
 print('Hey, how are you?')
 feeling=input('')
 if feeling == "good":
  print('Thats good!')
 if feeling == "bad":
  print('aww thats bad, maybe chatting with me will help?')
#Restarting The script
os.system('python main.py')

Just for some extra info, im using python 3 running scripts via ubuntu root terminal All help is greatly appreciated. Thanks.

kabanus
  • 24,623
  • 6
  • 41
  • 74
Scorch
  • 115
  • 5

1 Answers1

-2

I guess you're using python2 to run the script. Try python3 instead.

I'm able to replicate your error by running it with python2

Traceback (most recent call last):
  File "temp", line 4, in <module>
    talk=input('')
  File "<string>", line 1, in <module>
NameError: name 'Hi' is not defined

But success by using python3

Hi
Hey, how are you?
  • how could you run Python 3 print in Python 2? – Michał Zaborowski Aug 06 '18 at 08:06
  • 1
    @MichałZaborowski to answer your question directly, you could `from __future__ import print_function`. But that's not necessary for `print('something')` to work just fine in Python 2.x. Things start getting surprising once you have `print('two', 'things')`, but it still runs just fine. – jonrsharpe Aug 06 '18 at 08:49
  • @kavinvin - I think you missed the point. There is particular piece of code, which is not working in Python 2.X - yes? So assumption that someone used the code this way is not valid - or I'm missing something... – Michał Zaborowski Aug 06 '18 at 14:35
  • @MichałZaborowski I just pointed out that he may unintentionally use python2 (while he say he's using python3) because his error shouldn't appear in python3 anymore. `input()` in python2 evaluate user input as python code the same way `exec()` does. If you look at his error closely, it said that `NameError: name '___' is not defined`. I guess he entered `___` as an input then python2 evaluate `___` as undefined variable. I identify his error directly as he requested, where did I miss the point? – Kavin Ruengprateepsang Aug 06 '18 at 17:09
  • I'm not sure if he's linked `python` in his path to `python3`. But by default, Ubuntu has `python` link to `python2`. In the last line, `os.system('python main.py')`, he's calling simply `python` which I guess it's calling `python2`? – Kavin Ruengprateepsang Aug 06 '18 at 17:27
  • You can't use python 2.X by accident with this code. First step of processing is parsing, and that fails since print is not language keyword, but function. Maybe I was not precise at first place. Also that kind of comment is OK for comments - not for answers. – Michał Zaborowski Aug 09 '18 at 08:14