python - input() error - NameError: name '...' is not defined -
i getting error when try run simple python script:
input_variable = input ("enter name: ") print ("your name is" + input_variable)
lets type in "dude", error getting is:
line 1, in <module> input_variable = input ("enter name: ") file "<string>", line 1, in <module> nameerror: name 'dude' not defined
i running mac os x 10.9.1 , using python launcher app came install of python 3.3 run script.
edit: realized somehow running these scripts 2.7. guess real question how run scripts version 3.3? thought if dragged , dropped scripts on top of python launcher app inside python 3.3 folder in applications folder launch scripts using 3.3. guess method still launches scripts 2.7. how use 3.3?
tl;dr
input
function in python 2.7, evaluates whatever enter, python expression. if want read strings, use raw_input
function in python 2.7, not evaluate read strings.
if using python 3.x, raw_input
has been renamed input
. quoting python 3.0 release notes,
raw_input()
renamedinput()
. is, newinput()
function reads linesys.stdin
, returns trailing newline stripped. raiseseoferror
if input terminated prematurely. old behavior ofinput()
, useeval(input())
in python 2.7, there 2 functions can used accept user inputs. 1 input
, other 1 raw_input
. can think of relation between them follows
input = eval(raw_input)
consider following piece of code understand better
>>> dude = "thefourtheye" >>> input_variable = input("enter name: ") enter name: dude >>> input_variable 'thefourtheye'
input
accepts string user , evaluates string in current python context. when type dude
input, finds dude
bound value thefourtheye
, result of evaluation becomes thefourtheye
, gets assigned input_variable
.
if enter else not there in current python context, fail nameerror
.
>>> input("enter name: ") enter name: dummy traceback (most recent call last): file "<input>", line 1, in <module> file "<string>", line 1, in <module> nameerror: name 'dummy' not defined
security considerations python 2.7's input
:
since whatever user types evaluated, imposes security issues well. example, if have loaded os
module in program import os
, , user types in
os.remove("/etc/hosts")
this evaluated function call expression python , executed. if executing python elevated privileges, /etc/hosts
file deleted. see, how dangerous be?
to demonstrate this, let's try execute input
function again.
>>> dude = "thefourtheye" >>> input("enter name: ") enter name: input("enter name again: ") enter name again: dude
now, when input("enter name: ")
executed, waits user input , user input valid python function invocation , invoked. why seeing enter name again:
prompt again.
so, better off raw_input
function, this
input_variable = raw_input("enter name: ")
if need convert result other type, can use appropriate functions convert string returned raw_input
. example, read inputs integers, use int
function, shown in this answer.
in python 3.x, there 1 function user inputs , called input
, equivalent python 2.7's raw_input
.
Comments
Post a Comment