Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
School of Computing Science Simon Fraser University CMPT 120 - Introduction to Computing Science and Programming Term: Fall 2012-3 Instructor: Bill Havens Assignment #1: Basic Python Programming DUE DATE: Friday, 2012 September 21 @4pm submitted electronically to the Submission Server. Warning: Try the server before the deadline to make sure it works for you. INSTRUCTIONS: Provide solutions to these questions in Python. Make sure you test your code on the given examples plus others to make sure they work properly. Turn in the source files and the transcripts of your tests to the Submission Server as a ZIP file. Have fun! 1. Factorial Function Consider the factorial(n) program of figure 3.5 in the Study Guide. Implement this function and test it on both small and large positive values of ānā. Notice how the size of the output grows very quickly as n increases. 2. While loops vs. For loops Figure 3.5 implements the factorial(n) program using a For loop. Rewrite the program to use an equivalent While loop instead. Test your function. 3. Simple Interest Calculator Suppose you invest a lot of money in the bank which pays interest every year on your investment. Write a program which reads the principal amount to be invested, P, and then the interest rate per annum, I. The total value of your investment at the end of the year will be P * (1.0 + I) . Ask the user for the principal amount and the annual interest rate, then print out the value of the investment after one year. Test your program on various principals and interest rates. Currently a reasonable interest rate is only 3% which is specified as the rate 0.03. 4. Compound Interest Calculator Suppose you leave your money in the bank for more than one year and you add the interest to the principal each year. Then the principal P will grow each year as specified by the formula: P = P * (1.0 + I). Write a program as above that asks for a principal starting amount P and an interest rate I and also a number of years "n" to invest. Use a FOR loop to find the total value of your investment after "n" years. Print out the principal P after each year and stop after the "n"th year. Given an interest rate of 6%, how many years does it take to double your money? 5. Runners calorie counter Runners are very concerned about their caloric expenditure. Actually not the best way to loose weight but nevertheless running does burn calories. Turns out that a runner burns about 100 calories per mile. The further you run, the more calories you loose. OK, now your job is to write a program that tells a runner how many calories they have burned given the inputted distance that they ran. Ask the user how many miles they ran and then ouput the equivalent calorie burn. But Canada uses the metric system (like most of the civilized world!). So modify your program so that it asks the user whether to use miles or kilometres to measure distance. Show your calorie counter working on various distances specified in both miles and kilometres. Demonstrate that it works for distance equal to zero.