{article Examples__Python 3.4 }{title} {text}{/article}

NameError: name 'raw_input' is not defined

In python 3.x, there is only one function to get user inputs and that is called input, which is equivalent to Python 2.7's raw_input.

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:

>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

{source}
<!-- You can place html anywhere within the source tags -->

<pre class="brush:py;">
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
>>>
</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> fname = input('Enter your name: ')
Enter your name: Bill
>>> print('Hello', '!')
Hello !
>>> print('Hello', fname, '!')
Hello Bill !
>>>

{source}
<!-- You can place html anywhere within the source tags -->

<pre class="brush:py;">

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> fname = input('Enter your name: ')
Enter your name: Bill
>>> print('Hello', '!')
Hello !
>>> print('Hello', fname, '!')
Hello Bill !
>>>

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

>>> num1 = int(input('Enter a number: '))
Enter a number: 2
>>> num2 = int(input('Enter a number: '))
Enter a number: 2
>>> print(num1 + num2)
4
>>>

{source}
<!-- You can place html anywhere within the source tags -->

<pre class="brush:py;">

>>> num1 = int(input('Enter a number: '))
Enter a number: 2
>>> num2 = int(input('Enter a number: '))
Enter a number: 2
>>> print(num1 + num2)
4
>>>

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}