Labs in this course are designed to be completed using the skills and tools we discuss in class. You shouldn't need to google for advanced topics, read ahead in the book, or ask a CS major. You can ask for general help using these resources, but be sure to cite them as a comment in your lab. Often, using an advanced feature of python does not stress the core problem solving skills the lab is designed to strengthen. Yes s[::-1] reverses a string in python, but attempting this using a string accumulator will improve your problem solving skills.
In CS21, you must complete labs submitted for a grade on your own. You may not email, print, or otherwise show lab code to another student. You may not write code for another student or have anyone write code for you. You may not review another student's lab code prior to the deadline. In upper level courses, you are encouraged to work with one or more partners on collaborative assignments, but in CS21, we need to ensure that individuals are capable of problem solving on their own.
During in class assignments, and quiz studying, you are encouraged to collaborate and share ideas, but you this collaboration is prohibited on material submitted for a grade.
Sometimes we want the computer to pick a random number in a given range, pick a random element from a list, pick a random card from a deck, flip a coin, etc. The random module provides access to functions that support these types of operations. The random module is another library of functions that can extend the basic features of python. The math modules is another example of extending core python functionality. For a full list of python modules, see the online documentation listing all of the default modules. To get access to the random module, we add from random import * to the top of our program (or type it into the python shell).
Open the file randOps.py in emacs or vim, and run the program in a separate terminal. Note if you run the program again, you get different (random) results. This program illustrates the functions randrange and random. We will most often be using randrange. This function generates a list just like range, but then returns one item randomly from that list.
def <function name>( <parameters>): """ <descriptive comment> """ <body>When using a function, you should treat a function as a black box that takes input, performs some well defined task with that input, and possibly returns some output. If you understand what the function is supposed to do, you do not need to understand how the function actually performs that task. Think about some functions that you have used already. What do they do? How do they work? You may not know the answer to the second question, and you do not need to know the answer. You should keep this point in mind when writing your functions. Users should not need to know how a function works to simply use the function.
Functons have a number of uses: