Colorado State University Logo | CS 150B - Culture and Coding: Python (AUCC 3B/GT-AH3) Colorado State University Logo | CS 150B - Culture and Coding: Python (AUCC 3B/GT-AH3)
CS 150B - Culture and Coding: Python (AUCC 3B/GT-AH3)
Computer Science

Lab 01 - Functions & Expressions

Introduction

Throughout history, computers have been used to process data, and to make math problems such as cryptography easier for humans. While their use today has extended far beyond math, the goal of a computer is to make our lives easier, and the goal of the computer scientist is to use technology to improve the world around them.

Going back to the simple uses of a computer, you are going to build a simple program that uses both functions and basic operations to answer arithmetic questions. While simple, it is important to pay attention to how we are using the Divide-Conquer-Glue approach breaking up each part of the program into small simple pieces. It would be simple to add another math operation.

In this lab you will learn:

  • Basic Operations
  • More printing
  • Using functions to divide a problem into smaller parts.

Provided Code

You will notice we provided two functions for you. main and run. You should not modify anything in the run function, but we encourage you to read through it. The run function is the ‘glue’ in the Divide-Conquer-Glue strategy.

What’s in a Name (Step 0)

For all labs, you will want to modify your name and email by the author line

## @author YOUR_NAME
##         YOUR_EMAIL

You should do this now!

Run Your Program (Step 0.5)

To run your program, make sure you are in the ‘Develop Mode’ and click run. This won’t appear to do anything, but you should type 0 in the “Enter program input (optional)” text box in zybooks. Doing so will make sure the program ends.

One should never write an entire program and then run it. They should instead run frequently and without reservation making sure the lines you type are indeed correct.

REMINDER: You should be running your code as you work - just to see what is happening.

Add Numbers (Step 0.75)

Find the add function. This function has already been written for you but you should make sure that you understand what is happening within the function before completing the following functions.

The function signature for the function is:

    def add(num1,num2):

The keyword def is used to define a function add is the name of the function. num1 and num2 are the parameters. They are variable of type int that can be accessed anywhere within the add function.

Note: num1 and num2 are ints because that is the input type we are asking for.

Inside the function the following occurs:

    add = num1 + num2
    return add

First a variable called add is declared. That variable is then assigned with the solution of adding the two parameters (num1 and num2) together. add is then returned.

This function will not be graded. If you want to test this function, you can uncomment add(3,2) in main to test adding 3 + 2.

Hint: Use this function as a reference while completing the following function.

Subtract Numbers (Step 1)

Find the subtract function. You will notice the following function signature.

def subtract(num1,num2):

num1 and num2 are parameters or variables that are ‘passed’ into the function. You can use them.

You should subtract num1 minus num2, and return the result. Remember, to run and test it.

For example:

  • If 5 and 2 are passed in, the result returned should be 3.
  • If 10 and 20 are passed in, the result returned should be -10.
  • If 2 and 1 are passed in, the result returned should be 1.

To get you started, you can copy and past the following into your Enter program input (optional) box

2
5 
2
2
10 
20
2 
2 
1
0

That will run every test above, and then exit the program. You should change it and run your own tests. Remember, programming is like playing a musical instrument, you only learn by practicing.

Multiply Numbers (Step 2)

Find the multiply function.

The purpose of the multiply function is to multiply num1 by num2, and then return the result.

For example:

  • If 5 and 5 are passed in, the result returned should be 25.
  • If 10 and 20 are passed in, the result returned should be 200.
  • If 2 and 1 are passed in, the result returned should be 2.

Once this function is written, you can test the program by running it.

Divide Numbers (Step 3)

Find the divide function. You should divide num1 by num2, and return the result. Remember, to run and test it.

For example:

If 5 and 1 are passed in, the result returned should be 5.

Exponential Numbers (Step 4)

Find the exponential function. You should calculate num1 to the power of num2, and return the result.

For example:

  • If 2 and 3 are passed in, the result returned should be 8.
  • If 1 and 20 are passed in, the result returned should be 1.
  • If 4 and 2 are passed in, the result returned should be 16.

Many programs have menu’s of actions to perform. However, the only way to know the options is to tell your client what they are allowed to do. Locate the printOptions TODO comment. This function simply prints several options, and that is it.

You first must write the fucntion signature for this function below the TODO comment. The function is called printOptions(), and it does not return anything.

The options you should print are as follows:

Menu Options
1) Add 
2) Subtract 
3) Multiply
4) Divide
5) Exponential
0) Exit
Select a menu option:  

However, before you print the Menu Options you should print two blank lines. Additionally, the Select a menu option line should have a space at the end.

After, you have completed the printOptions() function uncomment run() in the main function. run() will call the printOptions() function, so you will be able to see your menu you just wrote.

Thinking further

If you wanted to write a function that ‘squares’ a number, and only takes in one number - how would you go about doing that?

Turning In (Step 6)

Make sure you click through the canvas link to the assignment if you haven’t already. This triggers the linking process, so canvas can get an updated score. As with the previous assignment, you only have five changes to ‘submit for grading’, so it is important to run the program testing it for various cases before you click submit for grading.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

CS 150B - Culture and Coding: Python (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.