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 cone.py, that asks the user to enter the value of the radius and the value of the hight of a cone. Your program will then compute and print out the volume and the surface area of the cone using these formulas:
Here is what two runs of your program might look like:
$ python cone.py This program computes the volume and surface area of a cone given values for the height and radius Enter the radius value: 4 Enter the height of the cone:3 A cone with radius 4 and height 3 has a volume of 50.265 and a surface area of 62.832 $ python cone.py Enter the radius value: 2.3 Enter the height of the cone:0.8 A cone with radius 2.3 and height 0.8 has a volume of 4.432 and a surface area of 17.596Try using string formating to limit the number of places printed beyond the decimal point. Actually, don't worry about this. We haven't covered it yet.
To use math library functions, remember to add this to the top of your program:
from math import *
Write a program named oddsum.py, that takes as input a positive integer value n, and then computes the sum of the first n odd integer values. For example, if the user enters 5, your program will compute the sum of 1, 3, 5, 7 and 9.
A couple runs of your program may look like this:
$ python oddsum.py This program computes the sum of the first n odd integers Enter a value a value for n: 5 the sum of odd integers between 1 and 10 is 25 $ python oddsum.py This program computes the sum of the first n odd integers Enter a value a value for n: 30 the sum of the odd integers between 1 and 60 is 900It is easy to test your program for correctness because the sum of the first n odd integers is equal to n squared. However, your program should not just compute n squared. Instead, you must use a loop to compute the sum using successive additions.
$ python reverse.py This program produces the reverse of a string entered by the user Enter a phrase: hello there The phrase reversed is ereht olleh $ python reverse.py This program produces the reverse of a string entered by the user Enter a phrase: a man a plan a canal panama The phrase reversed is amanap lanac a nalp a nam a
$ 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 * * * * * * * * * * * * * * * * * * * * * * * * *