Compsci 101, Fall 2017, Totem Howto

In this howto there's a discussion about getting started, Totem standards you must adhere to, raw strings, and random/if.

NOTE: Every Python program you write should have at least one comment in every function. Longer functions should have a comment for each section. See the main assignment web page for descriptions of what every python program should have in it. Functions on this page have comments.

Getting Started

Create a new Pydev project. You can call the project by any name, but we suggest f17-cs101-assign2-totem to help you identify the project in your workspace. You must create a Python module named HeadPoles.py and write functions in that module. The code you write must conform to the totem standards below. When you create the module, let Eclipse create the if __name__ == '__main__' boilerplate for you by selecting the option "Module: Main" when you create the new Python Module from the popup.

TIP: Write a few functions at a time and run your program to see if they work. Then add more functions and test them. Your program will be easier to debug if you do this! If you do have errors, always fix the first error in the file first. The others might go away when you do.

Totem Standards

  1. You will write head-part functions for parts of a head. See the requirements on the previous page for specific parts you must include and how many head-part functions you must write.

  2. Each head-part function that you write to draw a part of a totem should have one of the following labels as the first part of the function name. Every head-part function you write should begin with one of these strings. Please do not write helper functions that these call, even if such functions would make it simpler for you to write these functions. Please do not write nested/inner functions either. Each head-part function you write should start with one of the labels below, then have an underscore character, then some "meaningful" description of the function.

    For example head-part function names can be chin_cleft, hair_bald, mouth_open, or mouth_v1. You cannot use a name like mouth or earClogged because these don't conform to the naming specification. The "meaningful" description can be anything.

  3. Each head-part function must return a string that is fifteen characters "wide". The string can be a mult-line string, where each line is fifteen characters in length. e.g., you can write the function hair_part below: def hair_part(): # returns parted hair with the left side slanted a1 = r"123456789012345" a2 = r"|||||||||||||||" a3 = r"\\\\\\\||||||||" return a2 + "\n" + a3 To be more precise, the string returned by each of these functions must have length 15*k + (k-1) where k is the number of lines represented by the string. A one-line string will be exactly 15-characters, a two-line string will be two lines of 15 separated by one new line character so it's length will be 31 = 15*2 + (2-1) characters

    Some of the characters in each line can be spaces, but each must have length fifteen as a string (again, multi-line strings are fine).

  4. Each of the head-part functions whose name is based on one of the parts above must return a string.
  5. You will write two functions that don't return strings, but instead print the strings that make up a totem-pole. One of these functions should be named totemHeads and another should be named randomHeads. The first will print the same totem-pole each time it is called. The totem-pole must consist of at least three different "heads". You can also write functions to create individual heads that print the values returned by the so-called parts functions.

    The function, randomHeads, should create different poles each time it is called based on using the Python random module and using if/elif statements. Beards should be randomly added to the head or not added. (so it is random as to whether a head has a beard).

  6. You should use the main Python idiom we've seen to run both totemHeads and randomHeads when the program is run, e.g., from Eclipse: if __name__ == "__main__": # main function to print a totem pole # with three heads followed by a random totem pole print "My totem pole" totemHeads() print print print "My random totem pole" randomHeads()

Creating and Printing Strings

All strings you return and print should use what Python calls "raw" Strings. Strings preceded by a letter r before the double quote that begins the string will result in the string being printed without the need for any escape backslashes. For example:

print "\" generates an error in Python because the second quote has been escaped, however print "\\" will print one backslash. To avoid the need to line things up when backslashes are escaped, use raw strings as shown in the example below. print r"| \\// |" print r"| --- |" the letter 'r' before the string means that whatever follows the 'r' will be printed exactly as shown, no characters will be interpreted as escape characters or in any other way, they'll simply be printed. You can combine raw strings with other characters by concatenting them in one line as shown below for the function hair_parted that conforms to the totem standards.
    def hair_parted():
        # returns parted hair with the two sides slanted
        return " "*3 + r"\\\\\\\\/////"
Note that in this example, 3 spaces are concatenated to 15 characters in a raw string because " "*3 is a three-space string. There is one exception to using raw strings. You cannot end the string with a single backslash (or actually an odd number of backslashes). If you want to end the string with a single backslash (or odd number of backslashes) then add a blank at the end and it is ok to be of length 15+1 blank, so length 16 for that string. Here is an example. Use the second a2 line.
   def hair_pointy():
       # returns pointy looking hair
       a1 = r"123456789012345"
       a2 = r"/\/\/\_/\/\/\/\"    # THIS GIVES A SYNTAX ERROR because it ends
                                    # in a single backslash
       a2 = r"/\/\/\_/\/\/\/\ "   # Instead use this line which has a 16th 
                                    # character that is a blank after the backslash
       a3 = r"|             |"
       return a2 + "\n" + a3

Random if/else

To use random numbers you must import the random library and then call the function random.randint(low,high). This function returns a random integer between low and high inclusive, that is it returns x where low <= x <= high.

When you use an import statement, it is best to put them near the top of the file before any defs or other code so that any of your code can use the imports.

The example below returns three different strings with equal probability.

def random_chin(): # This function randomly selects and returns one of # three types of chins choice = random.randint(1,3) if choice == 1: return chin_cleft() elif choice == 2: return chin_beard() else: return chin_pointy()

We haven't covered if/elif/else statements yet but you can read ahead in the CompSci 101 textbook the first four sections of the Selection Chapter which you need to complete a reading quiz for by the time this assignment is due.

What does totemHeads() look like:

The function totemHeads should print three heads and might look like (assuming these are functions you wrote):

def totemHeads():
    # prints head 1
    print hat_tall()
    print hair_plain()
    print eyes_slanted()
    print nose_big()
    print mouth_small()
    print chin_plain()
    # heads 2 and 3 follow and are not shown

if __name__ == '__main__':
    # main function to print a totem pole 
    # with three heads followed by a random totem pole 
    print "My totem pole" 
    totemHeads()     # just call a printing function
    print 
    print 
    print "My random totem pole" 
    randomHeads()

OR ANOTHER WAY. You might have created functions for each head called head1(), head2() and head3(). Then you might have:

def head1():
    # print one head 
    print hat_tall()
    print hair_plain()
    print eyes_slanted()
    print nose_big()
    print mouth_small()
    print chin_plain()
    
def totemHeads():
    # print three heads, one after another, as a totempole
    head1()
    [rest of code not shown]


if __name__ == '__main__':
    # main function to print a totem pole 
    # with three heads followed by a random totem pole 
    print "My totem pole" 
    totemHeads()       # just call a printing function
    print 
    print 
    print "My random totem pole" 
    randomHeads()