Difference between revisions of "CSC111 Lab 4 2014"
(→Exercise 4: Investment and Interest Rate with PythonTutor) |
|||
Line 10: | Line 10: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | + | =Exercise 1= | |
* Observe the program below | * Observe the program below | ||
* Once you have figured out how it works, enter it in Idle and run it. Verify that it runs correctly. | * Once you have figured out how it works, enter it in Idle and run it. Verify that it runs correctly. | ||
Line 26: | Line 26: | ||
|- | |- | ||
| | | | ||
− | === Challenge | + | === Challenge 4 === |
|} | |} | ||
[[Image:QuestionMark3.jpg|right|120px]] | [[Image:QuestionMark3.jpg|right|120px]] | ||
Line 41: | Line 41: | ||
<br /> | <br /> | ||
− | + | =Exercise 2= | |
Write a Python program that asks the user to input the answer to a '''Yes/No''' question. The user is allowed to enter 'Y', 'y', 'yes', 'YES' for yes, and 'N', 'n', 'no', 'NO' for no. | Write a Python program that asks the user to input the answer to a '''Yes/No''' question. The user is allowed to enter 'Y', 'y', 'yes', 'YES' for yes, and 'N', 'n', 'no', 'NO' for no. | ||
Line 72: | Line 72: | ||
<br /> | <br /> | ||
− | + | =Exercise 3= | |
Write a program that reads a character for playing the game of Rock-Paper-Scissors. If the character entered by the user is not one of 'P', 'R' or 'S', the program keeps on prompting the user to enter a new character. | Write a program that reads a character for playing the game of Rock-Paper-Scissors. If the character entered by the user is not one of 'P', 'R' or 'S', the program keeps on prompting the user to enter a new character. | ||
<br /> | <br /> | ||
Line 79: | Line 79: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | + | =Exercise 4: Using PythonTutor= | |
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 114: | Line 114: | ||
|- | |- | ||
| | | | ||
− | === Challenge | + | === Challenge 5 === |
|} | |} | ||
[[Image:QuestionMark4.jpg|right|120px]] | [[Image:QuestionMark4.jpg|right|120px]] | ||
Line 141: | Line 141: | ||
− | + | =Exercise 5: Investment and Interest Rate with PythonTutor= | |
* Use PythonTutor to write this program. | * Use PythonTutor to write this program. | ||
Line 183: | Line 183: | ||
after '''4''' investment period, new balance = $1215.51 | after '''4''' investment period, new balance = $1215.51 | ||
− | ==Exercise | + | {| style="width:100%; background:silver" |
− | * Type or copy the program below in Idle. | + | |- |
+ | | | ||
+ | === Challenge 6 === | ||
+ | |} | ||
+ | [[Image:QuestionMark5.jpg|right|120px]] | ||
+ | |||
+ | <br /> | ||
+ | Make your program first prompt the user for the amount she would like the initial investment to grow. Then the program will print the growth of the investment each period until the | ||
+ | <br /> | ||
+ | =Exercise 6: Sentinels= | ||
+ | <br /> | ||
+ | We return to Idle with this program. However, you should feel free to use PythonTutor anytime you'd like to single step a program that is not behaving the way you want. It's a great tool to see the true logic of a program dynamically! | ||
+ | |||
+ | * Type or copy the program below in '''Idle'''. | ||
* Look at it and make sure you think you know how it will work | * Look at it and make sure you think you know how it will work | ||
* Run the program | * Run the program | ||
Line 209: | Line 222: | ||
|- | |- | ||
| | | | ||
− | === Challenge | + | === Challenge 7 === |
|} | |} | ||
[[Image:QuestionMark1.jpg|right|120px]] | [[Image:QuestionMark1.jpg|right|120px]] | ||
Line 222: | Line 235: | ||
|- | |- | ||
| | | | ||
− | === Challenge | + | === Challenge 8 === |
|} | |} | ||
[[Image:QuestionMark1.jpg|right|120px]] | [[Image:QuestionMark1.jpg|right|120px]] | ||
Line 229: | Line 242: | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 11:20, 18 February 2014
--D. Thiebaut (talk) 15:11, 17 February 2014 (EST)
This lab deals with while loops and introduces a new tool: PythonTutor.
Contents
Exercise 1
- Observe the program below
- Once you have figured out how it works, enter it in Idle and run it. Verify that it runs correctly.
x = input( "enter an integer between 3 and 9: " )
while 3 <= x <= 9:
print "invalid input!"
x = input( "enter an integer between 3 and 9: " )
print( "x = ", x )
Challenge 4 |
Can you add only 5 characters to the program and make it accept only numbers that are less than 3 or greater than 9?
Exercise 2
Write a Python program that asks the user to input the answer to a Yes/No question. The user is allowed to enter 'Y', 'y', 'yes', 'YES' for yes, and 'N', 'n', 'no', 'NO' for no.
Your program will contain a while loop that will force the user to keep entering an answer until it is one of the authorized inputs shown above.
You can use the skeleton program shown below as a starting point. Add different parts to make it robust
#--- ask user question ---
answer = input( "Do you like chocolate (Y/N)? " )
...
#--- keep on asking while invalid ---
while ... :
answer = input( "Please reenter: " )
...
#--- now that we know the answer, give feedback ---
if answer == ... :
print( "That's terrible" )
else:
print( "That's great!" )
Exercise 3
Write a program that reads a character for playing the game of Rock-Paper-Scissors. If the character entered by the user is not one of 'P', 'R' or 'S', the program keeps on prompting the user to enter a new character.
Exercise 4: Using PythonTutor
- Point your browser to http://www.pythontutor.com/ and click on Start Using Python Tutor
sum = 0
num = 1
while num <= 5:
sum = sum + num;
num = num + 1
print( "the sum of the numbers from 1 to %d is %d" % ( 5, sum ) )
- Enter the program above in the code window, select Python 3, and click on Visualize, as illustrated below. The image below is just an illustration. You should be working in another window or tab of your browser.
- In the new window that comes up, click on Forward and observe how the interpreter executes one line at a time and displays how the the variables change. We call this process single-stepping a program. In other word we force the interpreter to go one step (line) at a time.
- This is a great way to observe how small code sections operate!
- Single-step the whole program to see that it works correctly.
Challenge 5 |
The program below is supposed to compute the sum of all the even numbers
such that their sum is less than or equal to 20. Use PythonTutor to figure out the bug!
# buggy program that is supposed to compute
# the sum of even numbers and stop as soon as
# the sum is greater than or equal to 20. Your
# job is to fix the program using PythonTutor.
sum = 0
num = 0
while sum < 20:
sum = sum + num
num = num + 2
print( "the sum of the even numbers from 0 to %d is %d\n"
% ( num, sum ) )
Exercise 5: Investment and Interest Rate with PythonTutor
- Use PythonTutor to write this program.
- Below is a program that prints how a $1000 investment grows at a compounding rate of 5% over 4 periods.
initBalance = 1000.00
intRate = 0.05
print( "initial balance = $%1.2f" % initBalance )
newBalance = initBalance
newBalance = newBalance + newBalance * intRate
print( "after 1 investment period, new balance = $%1.2f" % newBalance )
newBalance = newBalance + newBalance * intRate
print( "after 1 investment period, new balance = $%1.2f" % newBalance )
newBalance = newBalance + newBalance * intRate
print( "after 1 investment period, new balance = $%1.2f" % newBalance )
newBalance = newBalance + newBalance * intRate
print( "after 1 investment period, new balance = $%1.2f" % newBalance )
- Replace the 4 identical pairs of statements with a while loop that will repeat one pair 4 times. The output of your program should be the same as the original program above.
- Change your program so that it prints the number of investment periods, as illustrated below:
initial balance = $1000.00 after 1 investment period, new balance = $1050.00 after 2 investment period, new balance = $1102.50 after 3 investment period, new balance = $1157.62 after 4 investment period, new balance = $1215.51
Challenge 6 |
Make your program first prompt the user for the amount she would like the initial investment to grow. Then the program will print the growth of the investment each period until the
Exercise 6: Sentinels
We return to Idle with this program. However, you should feel free to use PythonTutor anytime you'd like to single step a program that is not behaving the way you want. It's a great tool to see the true logic of a program dynamically!
- Type or copy the program below in Idle.
- Look at it and make sure you think you know how it will work
- Run the program
print( "please enter positive integers at the prompt. Enter -1 to stop" )
sum = 0
x = 0
while x != -1:
x = int( input( "Enter a positive integer (-1 to stop): " ) )
if x != -1:
sum = sum + x
print( "The sum of the numbers you entered is", sum )
Challenge 7 |
Modify the program above so that it stops when the sum of the numbers is greater than 20.
Challenge 8 |
Modify the program so that this time the program outputs the number of positive integers entered.