| CPSC 115L: Introduction to Computing | Spring 2010 |
Input: a temperature in CelsiusHow should Alyssa do this conversion? She knows that the freezing temperature 0 °C is equal to 32 °F, and the boiling temperature 100 °C is equal to 212 °F. From this, she computes the ratio of Fahrenheit to Celsius as follows: (212-32)/100 = 180/100 = 9/5. Then, using f for Fahrenheit and c for Celsius, the conversion formula must have the form f = 9/5 × c + k for some constant k. Since she knows that f = 32 when c = 0, this means that k must be equal to 32. In summary, she comes up with the following algorithm in pseudo code, a concise notation that combines elements of English and Python:
Output: the equivalent temperature in Fahrenheit
c = input('Enter a temperature in Celsius: ')
f = (9.0 / 5.0) * c + 32
print 'The temperature in Fahrenheit is', f, 'degrees.'
Lab Exercise 1. Modify Alyssa's solution to design and implement a Python script to do the exact opposite: convert a temperature given in Fahrenheit to the equivalent temperature in Celsius. For this, first write down on a piece of paper your I/O specification and algorithm in pseudo code. Then implement your algorithm in a Python script named f_to_c.py. Run your script with five test cases, including the special cases for the freezing point (32 °F) and boiling point (212 °F), and save the snapshots of your test runs in a text file named f_to_c.out (for this, use Text Editor under Accessories). Does your script gives the correct answers? When completed, show your work to the instructor or TA.
Exercise 2. Design and implement a Python script that will prompt the user for three exam scores and compute the course grade based on these scores. Assume that each of Exams 1 and 2 is worth 30% of the course grade, and Final Exam is worth 40% of the course grade. Assume further that the exam grades are integers between 0 and 100 and the course grade is a real number in the same range. Your script should behave as follows:
Enter the score of Exam 1: 87 Enter the score of Exam 2: 78 Enter the score of Final Exam: 77 Your course grade is: 80.3%
As before, first write down on a piece of paper your I/O specification and algorithm in pseudo code. Then implement your algorithm in a Python script named grade1.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named grade1.out.
Exercise 3. In Exercise 2, we assumed that each of Exams 1 and 2 is worth 30% and Final Exam is worth 40%. Now, modify your design and implementation so that the user can specify how much each exam is worth. Your script should behave as follows:
Implement your modified algorithm in a Python script named grade2.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named grade2.out. When completed, show your work to the instructor or TA.Enter the score of Exam 1: 87 How much is Exam 1 worth? 0.4 Enter the score of Exam 2: 78 How much is Exam 2 worth? 0.4 Enter the score of Final Exam: 77 How much is Final Exam worth? 0.2 Your course grade is: 81.4%
Exercise 4. If time permits and you would enjoy an optional challenge, then design and implement a simple calculator or converter for a problem or topic that interests you. For example, if you are a runner you might implement a program that converts kilometers to miles or calculates one's race pace in minutes per mile. If you are interested in health, you could implement a calculator for body mass index or a program that calculates calories consumed to different forms of exercise. It's up to you to search on line for the facts you need and for ideas on the I/O specifications.
As in the previous exercises, write down your I/O specifications and an outline of your algorithm and discuss these with the instructor or TA before you start to code. When completed, show your work to the instructor or TA.
# # File: f_to_c.py # Author: Takunari Miyazaki # Lab section: Tuesday # # Created: 02/02/10 # Modified: 02/02/10 #
|
|
CPSC 115L home page |
|
|