Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Math Modules
Math Module Intro

Objective

Check land deeds and property lines in the farm, verify and crunch down numbers by using Math Module functions.

In the office at the very edge of the main building there is an archive of old blueprints for the the building and land deeds. There are plans to expand the farm eastward but it’s important to get all the paperwork in order before you decide to look at any construction plans. This will be done by using python’s math module.

import math

Like the statistics module before it, the math module has a collection of math functions we can use to extend python’s built in library:

  • math.isnan(): Checks if value input is not a number

  • math.isinf(): Checks if value input is an infinite number

  • math.isfinite(): Checks if value input is finite

  • math.prod(): Multiplies all arguments added

  • math.pow(): Takes two arguments and returns the value of the first argument by the power of the second argument.

  • math.trunc(): Tunicates argument provided, this removes the decimal from a float point number, making it a whole number. (Does not round, just removes)

  • math.copysign(): Takes two arguments, copies the sign (-,+) from the second argument and applies it to the first one.

  • math.factorial(): Takes an argument and sums the multiplication of that number counting down to 1. For example, if the value is 5 it goes: 5 x 4 x 3 x 2 x 1 and produces a result.

We’ll be using these functions to review and chart down some data in order to prep the development projects. There are three list constants holding schematics for the farmland named: red_schematics , green_schematics , blue_schematics . These schematics have non numeric and unusable values written down, use the math functions to identify the elements, find the usable date so we can chart it down.

First walk to dark X mark over the red carpet, and use a loop to cycle through the elements of the red_schematics list constant in order to identify usable elements.

for x in red_schematics: y = math.isnan (float(x)) if y == True: player.speak("Not a Number") else: y = math.isinf(float(x)) if y == True: player.speak("Is infinite") else: player.speak(x)

The loop will cycle through all the elements of the list, the math.isnan() function will identify non numeric entries, the math.isinf() will identify infinite values that can not be used for the purpose of data collection. Create a new list named red_values and use a list comprehension to remove the undesired elements by employing math.isfinite() to filter them, like this: red_values = [x for x in red_schematics if math.isfinite(float(x))] .

Further add the numbers all together by using the math.prod() function to add together all the items in the list, like this: red_values = math.prod(red_values) .

Do this same filtering process on the dark X marks over the green and blue carpets, using the green_schematics , blue_schematics list constants and consolidating the elements in lists named green_values and blue_values . Walk to the gold X mark, face the desk and use the write() function that’s pre-written in the editor to chart down the usable schematic data. Once you’ve put everything together walk to the light X mark and use the open() function to access the next room.

Once in the next room, there are a couple of desks with some deeds to the land and coordinates. It’s a good idea to chart down some data before moving onto checking the new additions to the farm in the next area across the bridge. Walk to the light X mark in front of the crate with maps by the first desk, use the read() function to acquire index data for the land deeds.

Walk to the dark X mark over the purple carpet, create a variable named purple_data and use the math.pow() function to multiply the data you acquired to get coordinate information. Have the index by the power of 3 , like this: purple_data = math.pow( -index data- , 3 ) , be sure to add the index data you read before. Use math.trunc() to remove the decimal data from the variable, like this: purple_data = math.trunc(purple_data) . Once done, at the X mark, use the write() function pre-written in the editor with purple_data to chart down the data stored in the variable.

To finalize, walk to the light X mark in front of the filing cabinet next to the top desk. Use the read() function to acquire index data once again, this data needs to be unwrapped to reveal more coordinate information for the land. Create a variable named white_data to store the data value found in the file and a variable named sign to store the sign number found in the file. Walk to the dark X mark over the white carpet and use the math.copysign() function using the two variables you created as arguments and assign the value to white_data, like this: white_data = math.copysign(white_data,sign) .

Once done, with white_data, use math.factorial() to multiply the index, convert white_data to an integer and use it with the function, like this: white_data = math.factorial(int(white_data)) . Use the pre-written write() function to chart down white_data in order to complete the level.

Code book