python problems with getting user input [duplicate]
Clash Royale CLAN TAG#URR8PPP
python problems with getting user input [duplicate]
This question already has an answer here:
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.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
try
talk = input()
instead of talk=input(' ')
– Akshay Nevrekar
Aug 6 at 7:11
talk = input()
talk=input(' ')
@AkshayNevrekar how would the prompt or whitespace make any difference at all?
– jonrsharpe
Aug 6 at 7:13
1 Answer
1
I guess you're using python2
to run the script. Try python3
instead.
python2
python3
I'm able to replicate your error by running it with python2
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
python3
Hi
Hey, how are you?
how could you run Python 3 print in Python 2?
– Michał Zaborowski
Aug 6 at 8:06
@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 6 at 8:49
from __future__ import print_function
print('something')
print('two', 'things')
@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 6 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?– kavinvin
Aug 6 at 17:09
input()
exec()
NameError: name '___' is not defined
___
___
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
?– kavinvin
Aug 6 at 17:27
python
python3
python
python2
os.system('python main.py')
python
python2
please, improve your indentation.
– eyllanesc
Aug 6 at 7:09