When running the following code in terminal:
import getpass
import sys
import telnetlib
import time
user = input("Please Enter Your Username: ")
password = input("Please Enter Your Password: ")
ip = input("Please Enter RPi IP Address: ")
bot = telnetlib.Telnet(ip)
bot.read_until("login: ")
bot.write(user + "\n")
bot.read_until("password: ")
bot.write(password + "\n")
I get an error message saying:
Traceback (most recent call last):
File "con.py", line 6, in <module>
use = input("Please Enter Your Username: ")
File "<string>", line 1, in <module>
NameError: name 'pi' is not defined
P.S pi
is what was fed to the variable user
. It runs in python shell(until it gets to the telnet part but then it would obviously doesn't work in the shell). why wouldn't it run in the terminal?
Thanks
In Python 2, use raw_input()
instead of input()
to take string input from a user.
input()
tries to evaluate the input as a Python expression instead; a bare string is seen as a variable name in a Python expression, hence the NameError
. You could enter "pi"
instead, but that is not a great user-interface.
Demo:
>>> input("Please Enter Your Username: ")
Please Enter Your Username: pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'pi' is not defined
>>> raw_input("Please Enter Your Username: ")
Please Enter Your Username: pi
'pi'