Run update21, if you haven't already, to create the cs21/labs/02. Then cd into your cs21/labs/02 directory and create the python programs for lab 2 in this directory (handin21 looks for your lab 2 assignments in your cs21/labs/02 directory):
$ update21 $ cd cs21/labs/02 $ pwd /home/your_user_name/cs21/labs/02
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on the main page.
Write a program, in a file named pyramid.py, that asks the user to enter the length of the base and the height of a pyramid. Your program will then compute and print out the volume and the surface area of the pyramid using these formulas (A is the function for surface area, and V the function for volume in the figure below):
Here is what two runs of your program might look like:
$ python pyramid.py Thiss program calculates the volume and surface area of a pyramid. Please enter the length of the base: 8 Please enter the height: 3 The volume of the pyramid is: 64.0 The surface area the pyramid is: 144.0 $ python pyramid.py This program calculates the volume and surface area of a pyramid. Please enter the length of the base: 5 Please enter the height: 4.2 The volume of the pyramid is: 35.0 The surface area the pyramid is: 73.8773976394To use math library functions, remember to add this to the top of your program:
from math import *
Write a program named evensum.py, that takes as input a positive integer value n, and then computes the sum of the first n even integer values. For example, if the user enters 5, your program will compute the sum of 2, 4, 6, 8 and 10.
A couple runs of your program may look like this:
$ python evensum.py This program computes the sum of the first n even integers Enter a value for n: 5 the sum of even integers between 2 and 10 is 30 $ python evensum.py This program computes the sum of the first n even integers Enter a value for n: 30 the sum of the even integers between 2 and 60 is 930You should computer this sum using a loop.
$ python stars.py This program prints out a pattern of stars Enter a value for the size of the pattern: 5 - - - - - * - - - - * * - - - * * * - - * * * * - * * * * * $ python stars.py This program prints out a pattern of stars Enter a value for the size of the pattern: 8 - - - - - - - - * - - - - - - - * * - - - - - - * * * - - - - - * * * * - - - - * * * * * - - - * * * * * * - - * * * * * * * - * * * * * * * *Hint: start by writing a program that prints out these two patterns of stars, then think about how you can use these solutions to lead you to solving the above problem:
This program prints out a pattern of stars
Enter a value for the size of the pattern: 5
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
Write a program, morestars.py, that prints out the following patterns of stars given user input values for the size of the patterns:
This program prints out a pyramid of stars Enter a value of for the size of the pyramid: 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program prints out a diamond of stars Enter a value of for the size of the diamond: 5 * * * * * * * * * * * * * * * * * * * * * * * * *