Difference between revisions of "CSC111 While Loop Exercises"
(→Loop Exercises) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
==Loop Exercises== | ==Loop Exercises== | ||
;Exercise 1 | ;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. | + | : 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. |
;Exercise 2 | ;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. | + | : 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. |
;Exercise 3 | ;Exercise 3 | ||
Line 16: | Line 16: | ||
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. | ||
− | :Demo: count the number of times the | + | :Demo: count the number of times the words "Amherst" or "women" appear in [http://cs.smith.edu/~thiebaut/gutenberg/12241.txt Emily Dickinson's poems] |
;Exercise 5 | ;Exercise 5 | ||
Line 22: | Line 22: | ||
: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. | :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 | + | :The answer should just be a line stating something like: "To grow an initial investment of $1000 to $2000 at 4.5% will require |
<!-- | <!-- |
Latest revision as of 17:47, 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.
- 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.
- 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.
- 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