2014-04-20 160 views
0

當運行在終端以下代碼:NameError:名稱「___」沒有定義

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") 

我得到一個錯誤消息說:

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 

PS pi是什麼送給可變user。它運行在python shell中(直到它到達telnet部分,但它顯然不能在shell中工作)。爲什麼它不會在終端中運行?

由於

+0

'input'應該是'raw_input'。 – Blender

+0

@blender不適用於Python 3及以上版本? – crossboy007

+0

它看起來不像你在用Python 3執行腳本。你是如何運行它的? – Blender

回答

3

在Python 2,使用raw_input()代替input()採取來自用戶的字符串輸入。

input()嘗試將輸入評估爲Python表達式;在Python表達式中,裸字符串被視爲變量名稱,因此是NameError。您可以改爲輸入"pi",但這不是一個好的用戶界面。

演示:

>>> 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' 
相關問題