Difference between revisions of "CSC111 While Loop Exercises"

From dftwiki3
Jump to: navigation, search
(Loop Exercises)
(Loop Exercises)
Line 4: Line 4:
 
==Loop Exercises==
 
==Loop Exercises==
 
;Exercise 1
 
;Exercise 1
: Write a Python program that asks the user to enter a number that is greater than 0.  The function will keep on asking the user for the number until it is valid.  The function will return the number.
+
: Write a Python program that asks the user to enter an integer that is greater than 0.  The function will keep on asking the user for the number until it is valid.  The function will return the number.
  
 
;Exercise 2
 
;Exercise 2
Line 10: Line 10:
  
 
;Exercise 3
 
;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
 
: 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.
  
;Exercise 4
+
;
 +
:Make the program accept inputs until one player's score is more than 3 points ahead of the other.
 +
 
 +
;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
 +
 
 +
<!--
 +
<br />
 +
==Solutions==
 +
<br />
 +
<source lang="python">
 +
# Exercise 1
 +
x = 0
 +
while x == 0:
 +
    x = float( input( "Enter an integer greater than 0: " ) )
 +
 
 +
print( "You have entered:", x )
 +
 
 +
# Exercise 2
 +
answer = input( "Do you like ice cream? " )
 +
answer = answer[0]
 +
answer = answer.lower().strip()
 +
while answer != 'y' and answer != 'n':
 +
    print( "invalid input!" )
 +
    answer = input( "Do you like ice cream? " )
 +
 
 +
if answer=='y':
 +
    print( "I like ice cream too!" )
 +
else:
 +
    print( "This is so sad!" )
 +
   
 +
# Exercise 3
 +
human = input( "What do you play (R, P, S)? " )
 +
human = human.strip().lower()
 +
while human != 'r' and human != 's' and human != 'p':
 +
    print( "Invalid input!" )
 +
    human = input( "What do you play( R, P, S)? " )
 +
    human = human.strip().lower()
 +
 
 +
print( "You have played %s!" % human )
 +
 
 +
# Exercise 4
 +
line = input( "" )
 +
count = 1
 +
while line.find( "THE END." )==-1:
 +
    line = input( "" )
 +
    count = count + 1
 +
 
 +
print( "I have counted %d lines, including the sentinel line." % count )
 +
 
 +
# Exercise 5
 +
) )
 +
"""
 +
# initialize the 3 variables
 +
initBalance  = 1000 #int( input(  "Initial balance:    " ) )
 +
endingBalance = 2000 #int( input( "Ending balance:    " ) )
 +
intRate      = 0.05 #float( input(    "Rate (0.05 for 5%): " ) )
 +
 
 +
# start accumulating
 +
currentBalance= initBalance
 +
periods = 1
 +
 
 +
print( "starting balance: ", initBalance )
  
: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.
+
while currentBalance < endingBalance:
 +
    currentBalance = currentBalance * ( 1.0 + intRate )
 +
    #print( period, initBalance )
 +
    periods = periods+ 1
  
:Make the program accept inputs until one player's score is more than 3 points ahead of the other.
+
# correct last addition inside while loop
 +
periods = periods - 1
 +
print( "To grow $%d to $%d at a rate of %1.1f%% will take %d periods"
 +
      % ( initBalance, endingBalance, intRate*100, periods) )
  
  
 +
</source>
 +
-->
 
<!--
 
<!--
 
==Loop Exercises With Functions==
 
==Loop Exercises With Functions==

Revision as of 09:36, 17 February 2014

--D. Thiebaut 09:43, 10 November 2011 (EST)
revised --D. Thiebaut (talk) 21:55, 16 February 2014 (EST)


Loop Exercises

Exercise 1
Write a Python program that asks the user to enter an integer that is greater than 0. The function will keep on asking the user for the number until it is valid. The function will return the number.
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. The function will return True if the user entered Yes, and False otherwise.
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
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.

Make the program accept inputs until one player's score is more than 3 points ahead of the other.
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