Docsity

CS 1301 Spring 2008 Homework 1: Python Basics - Prof. Monica Sweat, Assignments of Computer Science

Toggle sidebar
bg1
For help, come to office hours and ask questions in workshop & lecture too! Page 1/6
CS 1301 – Spring 2008
Homework 1 – Your First Bite of Python
Due: Wednesday January 23, 2008 at 11:55 PM EST
Files to submit: warrior.py
kilometers.py
meters.py
sphere.py
For Help:
T-Square Forum – log on to T Square and click "Forums" and "HW1"
TA Help in person – hourly work schedule is posted on class website
Your workshop!
Email/chat with TAs
Notes:
Include your information as comments at the top of the HW1 file.
Include the collaboration statement (as outlined on the course syllabus)
Do not wait until the last minute to do this assignment.
If you find an error in the homework assignment, let a TA or Monica know immediately.
Part 1: Installing Python/IDLE
For Windows:
Windows users should download the Windows installer:
http://www.python.org/ftp/python/2.5.1/python-2.5.1.msi
Download and run the file above. Just follow the friendly instructions on screen to complete the
installation.
For Mac OS X:
Installation instructions depend on which version of Mac OS X you are using. If you are not sure,
which version of Mac OS X you're running, open "About this Mac" under the apple menu (below on
left). The version number is listed right below the logo (below on right).
If you are running 10.3.9 or 10.4 then you can download the Python 2.5.1 for Macintosh OS X.
If you are running only 10.3, then follow the instructions at: http://www.python.org/download/mac/
Installing Python and IDLE can sometimes be confusing, so if you are confused and have
questions, BE SURE TO ASK!!
pf3
pf4
pf5

Partial preview of the text

Download CS 1301 Spring 2008 Homework 1: Python Basics - Prof. Monica Sweat and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Spring 2008

Homework 1 – Your First Bite of Python

Due: Wednesday January 23, 2008 at 11:55 PM EST

Files to submit: warrior.py kilometers.py meters.py sphere.py For Help:  T-Square Forum – log on to T Square and click "Forums" and "HW1"  TA Help in person – hourly work schedule is posted on class website  Your workshop!  Email/chat with TAs Notes:  Include your information as comments at the top of the HW1 file.  Include the collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment.  If you find an error in the homework assignment, let a TA or Monica know immediately.

Part 1: Installing Python/IDLE

For Windows:

Windows users should download the Windows installer: http://www.python.org/ftp/python/2.5.1/python-2.5.1.msi Download and run the file above. Just follow the friendly instructions on screen to complete the installation.

For Mac OS X:

Installation instructions depend on which version of Mac OS X you are using. If you are not sure, which version of Mac OS X you're running, open "About this Mac" under the apple menu (below on left). The version number is listed right below the logo (below on right). If you are running 10.3.9 or 10.4 then you can download the Python 2.5.1 for Macintosh OS X. If you are running only 10.3, then follow the instructions at: http://www.python.org/download/mac/ Installing Python and IDLE can sometimes be confusing, so if you are confused and have questions, BE SURE TO ASK!!

Part 2: Let's Write Some Python!

READ THROUGH THE FOLLOWING PYTHON PROBLEMS CAREFULLY. All the solutions for this homework are python scripts and not functions. (Meaning you should not have a "def" statement anywhere in your code.)

Problem 1 – A Simple Script That Prints a Phrase. Save it in a file

called warrior.py

In 1978, a significant book in the history of Computer Science was published: The C Programming Language by Kernighan and Ritchie. The first example presented was a simple program that printed the words "hello world". Since then, the traditional first program written when learning a new programming language is Hello World. We will also implement a hello world program in Python. Open IDLE and follow this step-by-step. At the prompt (>>>), type 2 + 2 and hit enter. Your IDLE should look like this as you do that:

2 + 2 4

Now try this:

print "Hello, world." IDLE should display: print "Hello, world." "Hello, world."

Next let's record that print statement in the editor window of IDLE so that it can be saved. Open an editor window File->New Window from the Menu Bar (or pressing Ctrl-N). This plainer looking editor window is where you will type and save your programs. Enter the code that prints "Hello, world." and select File->Save as... Save your file as helloWorld.py (capitalization does matter). To execute the file (the script) either go to Run->Run Module on the Menu Bar (or hit F5). The interpreter should reappear and have printed "Hello, world." to the screen. Congratulations! You have just finished your first Python program. Now with the skills you have just learned, write a program called warrior.py that prints out the phrase, “I am a Python warrior” instead of “Hello, world.” Save it in a file called warrior.py. DO NOT USE FUNCTIONS IN THIS PROBLEM. It will be a script. (I.e. there will be no "def" line in your solution.)

Problem 3 – A Simple Script to Convert Inches to Meters. Save it in a file called meters.py Helpful info: Built-in Python Functions : http://docs.python.org/lib/built-in-funcs.html str (float or int) If you plan to use string concatenation (+) for this problem, then you will need to convert inches and meters to a string before concatenating. Be sure to only apply the str function when you are doing the printing – definitely not before you finish the calculation. Here's a quick example of how it works: Example:

age = 21 print age + " is not old." (this line would not work because it can't concatenate a number with a string.) print str(age) + " is not old." (this one will work.) Save as..._ Save your file as helloWorld.py (capitalization does matter). To execute the file (the script) either go to Run->Run Module on the Menu Bar (or hit F5). The interpreter should reappear and have printed "Hello, world." to the screen. Congratulations! You have just finished your first Python program. Now with the skills you have just learned, write a program called warrior.py that prints out the phrase, “I am a Python warrior” instead of “Hello, world.” Save it in a file called warrior.py. DO NOT USE FUNCTIONS IN THIS PROBLEM. It will be a script. (I.e. there will be no "def" line in your solution.) Problem 3 – A Simple Script to Convert Inches to Meters. Save it in a file called meters.py Helpful info: Built-in Python Functions : http://docs.python.org/lib/built-in-funcs.html str (float or int) If you plan to use string concatenation (+) for this problem, then you will need to convert inches and meters to a string before concatenating. Be sure to only apply the str function when you are doing the printing – definitely not before you finish the calculation. Here's a quick example of how it works: Example: >>> age = 21 >>> print age + " is not old." (this line would not work because it can't concatenate a number with a string.) >>> print str(age) + " is not old." (this one will work.) Write a script that prompts the user for a number of inches and prints the equivalent number of meters within a proper sentence. The script should do these three steps:

  1. prompt the user for the number of inches and store that answer
  2. calculate the equivalent number of meters (hint, 1 meter = 39.3700787 inch, so divide the inches by 39.3700787.)
  3. print the number of meters within a complete sentence, meaning have your script print something of this form: 30 inches equals 0.762 meters. Hint: make sure that you display the number of kilometers with 3 post decimal digits using the %f trick shown in class for number formatting. Example:

How many inches? 30 30 inches equals 0.762 meters. DO NOT USE FUNCTIONS IN THIS PROBLEM. It will be a script. (I.e. there will be no "def" line in your solution.)

Problem 4 – A Simple Script to Calculate the Volume of a Sphere. Save it in a file called sphere.py Write a script that calculates the radius of a sphere. The script should do these three steps:

  1. prompt the user for the radius and store that answer
  2. calculate the volume of the sphere having that radius. Note: the volume of a sphere is: Note we are dealing with python, so make sure you recall that 4/3 will be literally 1 in python, and that is not what you want. You need to use 4.0/3.0 in your program. Also, please use the pi constant from the math module as shown in class so that your calculation is fairly accurate.
  3. print the volume to 2 post decimal digits with a label, having this form: Volume: 104. Hint: make sure that you display the number of volume to only 2 post decimal digits using the %f trick shown in class for number formatting. Examples so that you can check your answer:

Please enter the radius of the sphere: 1 The volume of the sphere: 4.

Please enter the radius of the sphere: 2 The volume of the sphere: 33. DO NOT USE FUNCTIONS IN THIS PROBLEM. It will be a script. (I.e. there will be no "def" line in your solution.)