alright i know you're tired of explaining simple things to me, but i've looked around and really can't figure how to do this...

i'm trying to write a simple python script like when you type in a number for n, and press enter, it finds n+2?

i thought it will be something simple like

input (n)
print (n+2)

but apparently input doesn't define n, as it says "name n is undefined"

anyone know how to write this script?
or anyone know the command to define n?

anyone know the command to define n?

n = #something
# For user input use:
n = raw_input( 'This is a prompt: ' )

Ah just a simple misunderstanding. Firstly you can do it this way

n = input("Enter your number: ")
print(n+2)

But that wont work with python 30. Also in python 2.x people dont usually like the input() function as it evaluates the input. So if i typed 2*7 it would return 14 rather than just an 2 or 7. So what we use it

n = int(raw_input("enter your number: "))

But to make it work with python 30 we need to remember that with python 30 the input statement now is like the python 2.x raw_input statement so we can go:

n = int(input("Enter your number: "))
print (n+2)

Hope that solves your problem! :)

thanks, how did i not realize to put "n= input()"

not quite sure what the difference with int(raw_input) is from just input, but i'll figure it out...i notice command is in 2.6, but not in 3.0

but thanks, works great

print ("square any number!")
n = input("enter your number: ")
print (n*n)
execfile ("program.py")

execfile so that it doesn't exit and you can enter another number!

not quite sure what the difference with int(raw_input) is from just input, but i'll figure it out...i notice command is in 2.6, but not in 3.0

raw_input forces the user's input into a string data type, where as input actually parses the input... this should illustrate the difference:

>>> input( "Enter something: " )
Enter something: 4 + 7 * 10
74
>>> raw_input( "Enter the same thing: " )
Enter the same thing: 4 + 7 * 10
'4 + 7 * 10'
>>>

As you can imagine, it would be possible to enter malicious code using this method.

In 3.0 this "hole" was closed by making input() the new raw_input() and doing away with the parsing method... I hope that wasn't too confusing I know I didn't explain it well.

Check this out: it's Python Regrets, a good read.

commented: ah alright nice +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.