Due: Thursday, September 17th at class time
In this lab, we will leverage our understanding of the methods for getting user input and create programs to perform some calculations.
The objective of this lab is to develop a Number Guesser program that will:
Ask the user to think of an arbitrary secret number.
Note the user is supposed to keep the number secret from the program and the program is supposed to guess the user’s secret number.
Ask the user to multiply the secret number by 5, add 6, multiply by 4, add 9, and multiply by 5.
Note that it is the user who is supposed to perform those operations with his or her chosen number, not your program.
Ask the user to enter the result of the above operations.
Guess the secret number.
Output the guessed secret number.
A possible execution of your Number Guesser program may look like this:
- Think of a secret number.
- Multiply it by 5.
- Add 6 to the result.
- Multiply the previous result by 4.
- Add 9.
- Again, multiply by 5.
- Enter the final result: 4365
Hmmm... I guess your secret number was 42.
Start by using the the operations given above to create a mathematical equation of the form:
result = <calculation involing 'secret number'>
Then, use algebra to solve the above equation for 'secret number' in terms of result. Finally, create the given program. Be sure and execute the program on at least 4 trials by filling in the table below:
secret number | result | program output |
---|---|---|
42 |
4365 |
|
Put your program in a Python source file called NumberGuess.py.
In this problem, we'll think about converting units of time. Suppose we have some number of seconds (sec) and we want to convert that time into minutes and seconds (m and s) such that s < 60. For example, if sec = 137, then we'd like to compute m = 2 and s = 17, since 137 seconds is the same as 2 minutes and 17 seconds.
Write an expression that computes m from sec. Write another to compute s from sec. (Hint: use the / and % operators). Save these expressions in a text file called Modular.rtf.
Suppose we want to do the same with but with hours too. So we're given seconds (sec) but we want to calculate how many hours (h), minutes (m) and seconds (s) that is, where both m < 60, s < 60. Write expressions that compute each of h, m and s in terms of sec, and add these to the file Modular.rtf.
Now create a program called TimeConvert.py that asks the user to input a number of seconds and converts that time to a more readable version of hours, minutes and seconds. For example, if the user enters 137, the output should read
137 seconds is equal to 0 hours, 2 minutes and 17 seconds.
Or if the user enters 3601, the output should read
3601 seconds is equal to 1 hours, 0 minutes and 1 seconds.
Notice I'm not worried about "hours" vs. "hour".
This problem will be pretty similar to the previous one, except now we want to compute what change should be given. Create a program called ChangeMaker.py that prompts a cashier for a cost in cents and a payment in cents, and computes what coins and bills should be given back to the customer as correct change. For example, if the cost entered is $17.34 (entered simply as the integer 1734) and the payment is $20.00, the program should report:
Cost is 1734 centsIn this problem, we will create a program that computes an individual's income tax using some simplified versions of tax laws:
When the program is executed, it should ask the user for their gross income and the number of dependents. The output of the program is the amount of the income tax for the individual.
A sample execution might look like the following:
Enter the gross income: 150000.00
Enter the number of dependents: 3
The income tax is $26800.00
Put your solution in a Python source file named IncomeTax.py.
(Ch. 2, Prog. Ex. 17) You have seen that the math library contains a function that computes the square root of numbers. In this problem, you are to write your own algorithm for computing square roots. One way to solve the problem is to use a guess-and-check approach. You first guess what the square root might be and then see how close your guess is. You can use this information to make another guess and continue guessing until you have found the square toot (or a close approximation to it). One particularly good way of making guesses is to use Newton's method. Suppose x is the number we want the root of and guess is the current guessed answer. The guess can be improved by using the following expression as the next guess:
(guess + (x / guess)) / 2
Write a program that implements Newton's method. The program should prompt the user for the value to find the square root of (x) and the number of times (n) to improve the guess. Starting with an initial value for guess of x/2, your program should loop the specified number of times applying Newton's method and updating guess with the expression given above. Once the improvements are complete, the program should report the final value of guess. You should also subtract your estimate from the value of math.sqrt(x) and report how close it is. Run your program with at least 5 different values of x and at least 2 different values of n for each. Put your solution in a Python source file named SqRoot.py.
In your Assignment Inbox you should create a folder called Lab02 for your solutions. This should contain: