



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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.
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.
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!!
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.)
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:
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:
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.)