# 7.3: The input() Function

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

By Carey A. Smith.

The input() function lets the use enter data from the command window (aka console)
It displays a message and stores the user's input

##### Example $$\PageIndex{1}$$ Input a number:

hours_sleep = input('How many hours did you sleep last night? ')
The message is a character string. It is enclosed in single quotes.

This line of code expects a number to be entered.

The space after the ? puts a space between the message and the user's answer.

Solution

##### Example $$\PageIndex{2}$$ Input a character string

The default input is a number, so we need a 2nd parameter, 's', to tell input() to store a character string.

name2 = input('Enter the last of someone famous: ','s')

This line of code expects a character string to be entered.

Again, the space after the ? puts a space between the message and the user's answer.

Solution

The next example is from Troy Siemers, Section 6.1.

##### Example $$\PageIndex{3}$$ Input name and age

Here is a simple section of code to collect a person’s first name and their age.

>> FirstName = input('Enter your first name : ','s'); >> Age = input('Enter your age : ');

Once executed, the user enters values for each, say

Enter your first name : Bob

In this example, the 's' in the first input indicates that the program expects text input.

The second input (without the 's') indicates that the program expects numeric input.

When executed, the lines collect input one at a time. The space after the semicolon in each input() function statement separates what the user enters from the input() message.

When this code is run, each input() message is displayed and waits for the user to ente responses--in this case the name, Bob, and age, 25. Due to the semicolons, there is no output to the screen, but the values have been stored in the variables FirstName and Age (see the Workspace window to confirm).

Solution

This part is by Carey A. Smith

Review section 4.7 on why and how numbers are converted to characters strings in order to combine them in a message. It also explains concatenation.

##### Exercise $$\PageIndex{1}$$ Concatenation of more than 2 items

Put the following code into a script.

Run the code and enter answers to the input() questions.

first = input('Enter your first name: ','s') last = input('Enter your last name: ','s') year = input('Enter your year in college as a number: ') disp([first,' ',last,', college year= ',num2str(year)]) % The ' ' inserts a space between first and last.

% num2str(year) converts the number variable, year, to a character string. Each digit in the number is conveted a character using the ASCII standard. For details, see https://en.Wikipedia.org/wiki/ASCII.

Add texts here. Do not delete this text first.

## Homework:

##### Exercise $$\PageIndex{2}$$ Day of the week

Create a script file for the following:

[2 pts] Use the input() function to ask the user to enter the day of the week as a character string. The user's response is stored in a variable named day. Remember, that when the expected input value is a character string (not a number), you need to use the 's' optional argument, such as this:

day = input('Enter the day of the week: ', 's')

[2 pts] Use concatenation with the disp() function to display on the screen a message similar to this:

'Today is Monday'. (Use the variable day.)

Add texts here. Do not delete this text first.

.

##### Exercise $$\PageIndex{3}$$ Temperature

Create a script file for the following:

[2 pts] Use the input() function to ask the user to enter the temperature as a number, not a string. Don't use the 's' option for this. The user's response is stored in a variable named T.

[2 pts] Use concatenation with the disp() function to display on the screen a message similar to this:

'The temperature = 68 degrees'. Use num2str(T) to display the temperature.