Difference between revisions of "CSC111 Lab 4 2014"

From dftwiki3
Jump to: navigation, search
(Exercise 2)
Line 4: Line 4:
 
<br />
 
<br />
 
<br />
 
<br />
=Lab on While Loops=
+
<bluebox>
 +
This lab deals with '''while''' loops.
 +
</bluebox>
  
 
==Exercise 1==
 
==Exercise 1==
Line 22: Line 24:
 
|-
 
|-
 
|
 
|
== Challenge 1 ==
+
=== Challenge 1 ===
 
|}
 
|}
 
[[Image:QuestionMark3.jpg|right|120px]]
 
[[Image:QuestionMark3.jpg|right|120px]]
Line 48: Line 50:
 
<br />
 
<br />
 
<br />
 
<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.
 
<br />
 
<br />
 
<br />
 
<br />
Line 53: Line 58:
 
<br />
 
<br />
 
<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.
 
  
 
==Exercise 4: Sentinels==
 
==Exercise 4: Sentinels==
Line 64: Line 66:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
 +
 +
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 )
  
 
</source>
 
</source>
 
<br />
 
<br />
  
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
=== Challenge 2 ===
 +
|}
 +
[[Image:QuestionMark1.jpg|right|120px]]
 +
<br />
 +
Modify the program above so that it stops when the sum of the numbers is greater than 20.
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
=== Challenge 3 ===
 +
|}
 +
[[Image:QuestionMark1.jpg|right|120px]]
 +
<br />
 +
Modify the program so that this time the program outputs the '''number''' of positive integers entered.
 +
<br />
 +
 +
==Exercise 5:  ==
 +
<br />
 
Write a Python program that reads strings of characters until it finds the string "THE END.", in which case it stops.
 
Write a Python program that reads strings of characters until it finds the string "THE END.", in which case it stops.
 
The program will count how many times the word "mother" appears in all the strings entered.
 
The program will count how many times the word "mother" appears in all the strings entered.

Revision as of 19:28, 17 February 2014

--D. Thiebaut (talk) 15:11, 17 February 2014 (EST)





This lab deals with while loops.

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 1

QuestionMark3.jpg

Can you make your program only 3 characters longer 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 respond by 'Y', 'y', 'yes', 'YES' or 'N', 'n', 'no', 'NO'. The function keeps on asking until the user enters the correct information.

Your while loop should be the Python translation of the following sentence:

if the user's answer is not 'y' and it's not 'n', then keep on looping and asking for the user to enter a letter






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: Sentinels

  • 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 2

QuestionMark1.jpg


Modify the program above so that it stops when the sum of the numbers is greater than 20.




Challenge 3

QuestionMark1.jpg


Modify the program so that this time the program outputs the number of positive integers entered.

Exercise 5:


Write a Python program that reads strings of characters until it finds the string "THE END.", in which case it stops. The program will count how many times the word "mother" appears in all the strings entered.

Demo: count the number of times the words "Amherst" or "women" appear in Emily Dickinson's poems
Exercise 5
Write a program that asks the user for three pieces of information: a starting balance, a target balance, and an interest rate (entered as 0.05 for 5%, for example). The program then outputs the number of investment periods required for the starting balance to have grown larger than the target balance. While this can be computed directly mathematically, we want for this exercise to use a while loop to figure out the answer.
The answer should just be a line stating something like: "To grow an initial investment of $1000 to $2000 at 4.5% will require