Overview

Introduction

Welcome to the Reading Console Inputs and Formatting Outputs Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Creating a Simple Calculator

After completing this lab, you will be able to:

  • Accept console inputs and print formatted output in Python programs

Exam Objectives

The following exam objectives are covered in this lab:

  • 3.2 Construct and analyze code segments that perform console input and output operations

Lab Duration

It will take approximately 20 minutes to complete this lab.

Exercise 1 - Creating a Simple Calculator

Creating a simple calculator would involve accepting two numbers at runtime and then performing basic mathematical operations like addition, subtraction, multiplication, and division with those numbers.

The** input()** function helps you to read data at runtime. When the program encounters this function, it pauses and waits for a line input. When you type some characters and press enter, the input() function immediately returns the typed characters as a string.

For example:

aname = input("What name would you prefer for your pet?")

print(“Hello %s!” % aname)

This code will prompt you to enter a name and then print “Hello !” message.

Note that the result of the input() function is a string. However, if the program is expecting some other data type, you will need to convert the resultant string to that data type.

For example, the following code will convert the resultant string to an integer with the built-in type conversion function, int().

aage = int(input("What is your age?"))

print("Your age is: %d" % aage)

As you can see, the above two pieces of code use simple string (%s) and (%d) modifiers to format the output. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains ordinary text along with "argument specifiers", special symbols like "%s" and "%d". This type of formatting works best if the order of your arguments is not likely to change and you only have very few elements you want to concatenate.

A more advanced way to format the output is to use the format() function. The format() function reads the type of arguments passed to it and formats it according to the format codes defined in the string. The arguments of any type can be passed in any order.

For example:

print(“Hello {}, your age is {}”.format(aname, aage))

Assuming the name as “Rocky” and “15” as the age, this piece of code will print:

“Hello Rocky, your age is 15”

Now, if you enter “30” as the name and “Rocky” as the age. The code will simply print:

“Hello 15, your age is Rocky”

Unlike, the older means to format, the format() function takes any number of arguments. These arguments are divided into two types:

  • Positional: List of arguments that can be accessed with index of parameter inside curly braces {index}
  • Keyword: List of arguments of type key=value, that can be accessed with key of parameter inside curly braces {key}

To understand the difference better, take the same example.

print(“Hello {0}, your age is {1:d}.format(aname, aage)”)

Here aname is the 0th argument and aage is the 1st argument. The aname argument has no special formatting defined, whereas aage needs to be formatted as an integer.

Alternatively, you could rewrite the above statement using keyword parameters:

print (“Hello {aname}, your age is {aage:d}.format(aname=Elina, aage=30)”)

Here, instead of just the arguments, you have used a key value for the arguments—aname="Elina" and aage=30.

Both statements will expect the name as the string and the age as an integer. Note that here, since the special formatting has been defined, changing the order of arguments will result in an error.

In this exercise, you will learn to write a program to accept the desired mathematical operation and two whole numbers at runtime and then perform the basic mathematical calculations with those numbers as per the choice. The output will be appropriately formatted using the format() function and printed.

Learning Outcomes

After completing this exercise, you will be able to:

  • Accept console inputs and print formatted output in Python programs

Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.