ISF DP Computer Science

Methods #

This page contains information and coding exercises for methods.

Open up the pseudocode compiler in a new tab. You will be using this website to complete the exercises below.

Creating a Method #

method double(NUM)
   // this method doubles any number and returns the result
   NEW_NUM = NUM*2
   return NEW_NUM
end method

Running a Method #

OUTPUT_NUM = double(10)

Practice Exercises #

Exercise 1: Looping #

๐Ÿ’ป Create a method search() that checks for a certain number in a list
Parameters: a list of numbers, a number to look for
Return: True/False

  1. Create a method that loops through a list
  2. Receives a list of numbers as a parameter
  3. Returns true if the number is in the list
  4. Returns false if the number is not in the list

Exercise 2: Mad Libs #

The goal is to create a Mad Libs game, using methods, arrays, and conditionals. You can build it up step by step.

๐Ÿ’ป Create a method makeStory() that turns the story array into one long string
Don’t worry about the adjective and noun parts just yet.

input: the story array
return: one long string of all the words together

Remember, you can build up a string like this:

NAME = "Alex"

MYSTORY = ""
MYSTORY = MYSTORY + "Hello, "
MYSTORY = MYSTORY + NAME

Here are some examples you can use:

ARR = [ "In", "the", "adjective", "of", "the", "blazing", "noun" ]
ARR = [ "In", "the", "adjective", "of", "the", "blazing", "noun", ",", "there", "lived", "a", "noun", "named", "Dusty", "Jake",
    "whose", "plural noun", "had", "seen", "more", "sunsets", "than", "most", "plural noun", "see", "in", "a", "lifetime", ".",
    "With", "a", "adjective", "noun", "etched", "by", "the", "adjective", "winds", "of", "the", "plains", ",", "he", "rode",
    "his", "faithful", "animal", ",", "Thunder", ",", "across", "the", "vast", "adjective", "of", "the", "wild", "noun", ",",
    "chasing", "shadows", "of", "adjective", "and", "dreams", "of", "adjective", "gold", ".", "But", "beneath", "his", "adjective",
    "noun", "and", "adjective", "exterior", ",", "there", "beat", "a", "heart", "as", "adjective", "and", "free", "as", "the",
    "very", "noun", "he", "roamed", ",", "a", "noun", "that", "yearned", "for", "adjective", ",", "for", "the", "untamed", "spirit",
    "of", "the", "untamed", "noun", "." ]

๐Ÿ’ป Create a method getWord() that asks the user to input a word

input: TYPE. for example, adjective or noun
return: the word that the user types in


๐Ÿ’ป Now put them together

As you loop through each word in the makeStory(), if you encounter any of these: adjective noun plural noun animal, use the getWord() method to replace the word with the user’s choice.

Now you have a working Mad lib!

Extensions #

๐Ÿ’ป Generate your own stories Using AI (or your brain), generate stories. AI is reasonably good at converting a paragraph into an array. It’s less good at identifying parts of speech, but give it a try!

๐Ÿ’ป Repeat words Add a feature to your code where it can “remember” certain words and repeat them throughout the story, such as the main character’s name or catch phrase.

Deliverables #

โšกโœจ

Paste your code in your Exercises lab log

Complete the Exit Ticket