An Introduction to Python For Undergraduate Engineers/Getting Inputs

From Wikibooks, open books for an open world
Jump to: navigation, search

If you want your program to get an input from the user (for example to enter some parameters for your program to use), we use the input("Message here") command, as follows:

 number = input("Enter a number: ")
 username = raw_input("Enter your name: ")

When the program is run, this will show the user the message Enter your name: and will then save whatever the user inputs as the variable username. This can be very useful when writing a program to solve engineering type problems, as your program could ask the user to enter some parameters (such as mass, volume etc.) for your program to use.

In Python 2, input actually evaluates the input as python code. In the new version of Python, Python 3, input has the functionality of raw_input in Python 2. To replicate the functionality of input in Python 2, use eval(input).