Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

Tutorial 5: Interactive Script with input()

<< Previous Tutorial           Next Tutorial >>
On this page: input(), print(), + and * on string

Video Tutorial


Python 3 Changes

print(x,y) instead of print x, y

input() instead of raw_input()

Video Summary

  • Functions can be used to allow for interactivity between your program and the user. The function input() displays a displays a desired string to the user and asks for an input from them, while the function "print" displays a response.
  • In this tutorial, a = input('please type a word: ') is used to display the message please type a word: to the user. Upon the user entering a response and hitting the enter key, the subsequent function print((a+' ')*5) takes the string entered by the user, multiplies it by five, and gives that as a response. As seen in the previous tutorial, multiplication is carried out on a non-numeric string by printing it in a row the number it is being multiplied by: in this case, 5.
  • Remember order of operations when using Python. print(a+' '*5) will provide a different answer than print((a+' ')*5) because multiplication comes earlier within the order of operations.
  • Remember that Python will close a given program once all functions are completed within it.

Learn More

  • Note that the user input is always the string type, even when you enter a number. If you enter, say, '16', the result will be a string '16' rather than an integer 16. You can convert the string to an integer by using the int() function.

Practice

Write a script that prompts for a name and then prints out "Hello, XX!" where XX is the name, e,g,:
 
>>> What is your name? Homer
Hello, Homer! 
>>> 

Explore