Difference between revisions of "CSC111 Exercises on Loops"
(→Loop Exercises) |
(→Loop Exercises) |
||
Line 22: | Line 22: | ||
:Write a program that reads 2 characters from either the keyboard or a file. The characters are either PP, PR, PS, RP, RR, RS, SP, SR, SS. They correspond to the selections made by 2 players playing the game of rock-paper-scissors. | :Write a program that reads 2 characters from either the keyboard or a file. The characters are either PP, PR, PS, RP, RR, RS, SP, SR, SS. They correspond to the selections made by 2 players playing the game of rock-paper-scissors. | ||
− | Make the program accept inputs until one player's score is more than 3 points ahead of the other. | + | :Make the program accept inputs until one player's score is more than 3 points ahead of the other. |
;Exercise 6 | ;Exercise 6 |
Revision as of 09:01, 31 March 2010
Loop Exercises
- Exercise 1
- Write a function that asks the user to enter a number that is greater than 0. The function will keep on asking the user for a number until the number is correct. The function will return the number.
- Exercise 2
- Write a function 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 function that reads lines from a file until it finds a line that contains a particular string. The function receives the name of the file, and the string.
- Exercise 4
- Pi can be computed by adding the following terms (http://en.wikipedia.org/wiki/Pi):
- How many terms does it take to get the first 3 digits, 3.14?
- Exercise 5
- Write a program that reads 2 characters from either the keyboard or a file. The characters are either PP, PR, PS, RP, RR, RS, SP, SR, SS. They correspond to the selections made by 2 players playing the game of rock-paper-scissors.
- Make the program accept inputs until one player's score is more than 3 points ahead of the other.
- Exercise 6
- Replace the for-loops by while-loops in the following code
for c1 in 'ab': for c2 in 'abcdefghijklmnopqrstuvwxyz': print "111c-" + c1 + c2