Exercises#

Exercise 1: Rock-Paper-Scissors#

Implement a set of games of rock-paper-scissors against the computer.

  • Ask for input from the user (“rock”, “paper”, or “scissors”) and the randomly select one of these for the computer’s play.

  • Announce who won.

  • Keep playing until a player says that they no longer want to play.

  • When all games are done, print out how many games were won by the player and by the computer

Exercise 2: Pascal’s triangle#

Pascal’s triangle is created such that each layer has 1 more element than the previous, with 1s on the side and in the interior, the numbers are the sum of the two above it, e.g.,:

            1
          1   1
        1   2   1
      1   3   3   1
    1   4   6   4   1
  1   5   10  10  5   1

Write a function to return the first n rows of Pascal’s triangle. The return should be a list of length n, with each element itself a list containing the numbers for that row.

If you want to add complexity, write a function to print out Pascal’s triangle with proper formatting, so the numbers in each row are centered between the ones in the row above

Exercise 3: Panagrams#

A panagram is a sentence that includes all 26 letters of the alphabet, e.g., “The quick brown fox jumps over the lazy dog.”

Write a function that takes as an argument a sentence and returns True or False, indicating whether the sentence is a panagram.

Exercise 4: Math practice#

We want to make a simple table of trigonometric functions for different angles. Write a code that outputs in columns, the following data:

angle (degrees)    angle (radians)     sin(angle)     cos(angle)    sin(angle)**2 + cos(angle)**2

For all angles spaced 30 degrees apart in the range 0 to 360 degrees.

Keep in mind that the trig functions expect the input in radians.

Exercise 5: Calendar events#

We want to keep a schedule of events. We will do this by creating a class called Day. It is sketched out below. A Day holds a list of events and has methods that allow you to add an delete events. Our events will be instances of a class Event, which holds the time, location, and description of the event.

Finally, we can keep track of a list of all the Days for which we have events to make our schedule.

Fill in these classes and write some code to demonstrate their use:

  • Create a full week of days in your calendar

  • Add an event every day at noon called “lunch”

  • Randomly add some other events to fill out your calendar

  • Write some code that tells you the start time of your first meeting and the end time of your last meeting (this is the length of your work day)

class Day:
    """a single day keeping track of the events scheduled"""
    def __init__(month, day, year):
        # store the month, day, and year as data in the class
        
        # keep track of the events
        self.events = []
    
    def add_event(name, time=None, location=None):
        pass
    
    def delete_event(name):
        pass
    
    
class Event:
    """a single event in our calendar"""
    def __init__(name, time=9, location=None, duration=1):
        self.name = name
        self.time = time
        self.location = location
        self.duration = duration