Writing Programs

User Input

We can obtain input from the user, using Python's built-in function input.

Function input takes one argument, a string, which is a message printed to the screen. The function then pauses the program, until the user types 0 or more characters followed by the enter/return key. The text typed by the user is returned as a string.

Here is an example call on input:

In [1]:
name = input("What is your name? ")
What is your name? Jen

After the user eneters the text, input returns it as a string. In the statement above, variable name is assigned the value returned by function input:

In [2]:
name
Out[2]:
'Jen'

Now, let's ask the user to enter a number:

In [3]:
n = input("Enter a number: ")
Enter a number: 14

Variable n refers to a str value containing the text entered by the user:

In [4]:
n
Out[4]:
'14'
In [5]:
type(n)
Out[5]:
str

If we want to use the number as an int in our program, we could convert it:

In [6]:
n = int(n)
n
Out[6]:
14
In [7]:
type(n)
Out[7]:
int

Print

When working in the Python shell, we see the values of expressions after they have been executed. But instead of working in the Python shell, we usually write an entire program (a sequences of statements), save the program in a .py file, and then execute the program. When we run programs this way, we can print messages for the user, using Python's built-in function print.

Function print takes one or more values to be printed as argument(s). For example:

In [9]:
print('Hello, world!')
Hello, world!
In [10]:
print('hi', 'there')
hi there
In [11]:
print('hi', 5)
hi 5
In [12]:
print('computing', 'for', 'medicine')
computing for medicine

The values separated by commas are printed with spaces between them.

Perhaps, you don't want a space between the two strings that you are printing. You could use string concatenation to join the strings together and the resulting concatenated string would be printed:

In [13]:
print("C4" + "M")
C4M

We can also use variables in the expressions that we pass as arguments to print. Recall varaible name from earlier:

In [14]:
print(name)
Jen
In [15]:
print("My name is", name)
My name is Jen

Writing a program

Now, we will use input and print together in a program. We will write a program that:

  • prompts the user to enter two numbers,
  • computes the average of those two numbers, and
  • prints the average.

We start by using input twice to prompt the user for two numbers:

In [16]:
num1 = input('Enter a number: ')
num2 = input('Enter another number: ')
Enter a number: 12
Enter another number: 21

Variables num1 and num2 contain the text entered by the users as strings:

In [20]:
num1
Out[20]:
'12'
In [21]:
num2
Out[21]:
'21'

Before computing the average, we convert those values to floats:

In [23]:
average = (float(num1) + float(num2)) / 2

Finally, we print the result:

In [24]:
print("The average of", num1, "and", num2, "is", average)
The average of 12 and 21 is 16.5

Earlier, we wrote a function named pounds_to_kg to convert a weight in pounds to kilograms. Now, we want to write a program, where we ask the user of our program to enter a weight pounds, convert the weight to kilograms, and display the weight in kilograms to the user.

Next, we computer the average of the two numbers, but we first need to convert them from str to float:

Practice Exercise: Program to convert pounds to kilograms

Earlier, you wrote a function that takes a weight in pounds and returns that weight in kilograms. In a file named weight.py, write a program that:

  • prompts the user to enter a weight in pounds,
  • converts that weight to kilograms, and
  • prints the weight in kilograms.