Difference between revisions of "CSC111 Lab 4 Solutions 2014"
(Created page with "--~~~~ ---- <onlydft> =Solution Programs= <source lang="python"> # Solutions for Lab 4 # D. Thiebaut # =========================================================== # Exercise ...") |
|||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 20:31, 19 February 2014 (EST) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 20:31, 19 February 2014 (EST) | ||
---- | ---- | ||
− | + | ||
=Solution Programs= | =Solution Programs= | ||
<source lang="python"> | <source lang="python"> | ||
Line 137: | Line 137: | ||
</source> | </source> | ||
− | + | ||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 20:39, 20 February 2014
--D. Thiebaut (talk) 20:31, 19 February 2014 (EST)
Solution Programs
# Solutions for Lab 4
# D. Thiebaut
# ===========================================================
# Exercise 1
print( "\n========================\nExercise 1\n========================" )
x = int( input( "enter an integer less than 3 or greater than 9: " ) )
while 3 <= x <= 9:
print( "invalid input!" )
x = int( input( "enter an integer less tan 3 or greater than 9: " ) )
print( "x = ", x )
# ===========================================================
# challenge 1
print( "\n========================\nChallenge 1\n========================" )
x = int( input( "enter an integer between 3 and 9: " ) )
while not (3 <= x <= 9 ):
print( "invalid input!" )
x = int( input( "enter an integer between 3 and 9: " ))
print( "x = ", x )
# ===========================================================
# Exercise 2
print( "\n========================\nExercise 2\n========================" )
#--- ask user question ---
answer = input( "Do you like chocolate (Y/N)? " )
answer = answer.lower().strip()
#--- keep on asking while invalid ---
while answer != 'y' and answer != 'yes' and answer != 'n' and answer != 'no':
answer = input( "Please reenter: " )
answer = answer.lower().strip()
#--- now that we know the answer, give feedback ---
if answer == 'n' or answer == 'no' :
print( "That's terrible" )
else:
print( "That's great!" )
# ===========================================================
# Exercise 3
print( "\n========================\nExercise 3\n========================" )
human = input( "what is your play (R, S, P)? " )
human = human.strip().lower()
while human != 'p' and human != 's' and human != 'r':
human = input( "I didn't get that. Please reenter: " )
print( "You have played", human.upper() )
# ===========================================================
# Challenge 2
# 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.
print( "\n========================\nChallenge 2\n========================" )
# solution 1
sum = 0
num = 0
while sum < 20:
num = num + 2 # <--- fix by switching statements
sum = sum + num
print( "the sum of the even numbers from 0 to %d is %d\n"
% ( num, sum ) )
# solution 2
sum = 0
num = 0
while sum < 20:
sum = sum + num
num = num + 2
# num is always 2 larger that the associated sum
# decrease num to undo the last increment statement in the loop
num = num - 2
print( "the sum of the even numbers from 0 to %d is %d\n"
% ( num, sum ) )
# ===========================================================
# Exercise 5
print( "\n========================\nExercise 5\n========================" )
initBalance = 1000.00
intRate = 0.05
print( "initial balance = $%1.2f" % initBalance )
newBalance = initBalance
period = 0
while period < 4:
newBalance = newBalance + newBalance * intRate
period = period + 1
print( "after Investment Period %d, new balance = $%1.2f"
% ( period, newBalance ) )
# ===========================================================
# Challenge 3
print( "\n========================\nChallenge 3\n========================" )
initBalance = 1000.00
intRate = 0.05
print( "initial balance = $%1.2f" % initBalance )
newBalance = initBalance
period = 0
targetBalance = float( input( "What is your target balance? " ) )
while newBalance < targetBalance:
newBalance = newBalance + newBalance * intRate
period = period + 1
if period == 1:
plural = ""
else:
plural = "s"
print( "after %2d investment period%s, balance = $%6.2f"
% ( period, plural, newBalance ) )