Difference between revisions of "CSC111 Exercises on Loops"
(→Loop Exercises) |
(→Loop Exercises) |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __TOC__ | ||
==Loop Exercises== | ==Loop Exercises== | ||
− | + | ||
;Exercise 1 | ;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. | : 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. | ||
+ | |||
+ | :Example: | ||
+ | <tt>Please enter a number greater than 0: '''-1'''</tt> | ||
+ | <tt>Invalid number.</tt> | ||
+ | <tt>Please enter a number greater than 0: '''-10'''</tt> | ||
+ | <tt>Invalid number.</tt> | ||
+ | <tt>Please enter a number greater than 0: '''0'''</tt> | ||
+ | <tt>Invalid number.</tt> | ||
+ | <tt>Please enter a number greater than 0: '''10'''</tt> | ||
+ | ''(the function then returns 10 to the code section where it was called)'' | ||
;Exercise 2 | ;Exercise 2 | ||
Line 9: | Line 20: | ||
;Exercise 3 | ;Exercise 3 | ||
− | : Write a function that reads lines from a file until it finds a line that contains | + | : Write a function that reads lines from a file until it finds a line that contains the string "<<<END>>>". The function receives the name of the file and returns the lines read. |
;Exercise 4 | ;Exercise 4 | ||
Line 28: | Line 39: | ||
:Replace the for-loops by while-loops in the following code | :Replace the for-loops by while-loops in the following code | ||
− | :< | + | ::<source lang="python"> |
− | + | for c1 in 'ab': | |
− | + | for c2 in 'abcdefghijklmnopqrstuvwxyz': | |
− | + | print "111c-" + c1 + c2 | |
− | + | </source> | |
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 40: | Line 51: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | < | + | <onlydft> |
− | + | ==Solution Program== | |
− | def exo1( ): | + | <source lang="python"> |
+ | def exo1( ): | ||
ans = input( "Please enter a number greater than 0: " ) | ans = input( "Please enter a number greater than 0: " ) | ||
while ans < 0: | while ans < 0: | ||
ans = input( "Invalid input. Please enter a number greater than 0: " ) | ans = input( "Invalid input. Please enter a number greater than 0: " ) | ||
− | + | ||
return ans | return ans | ||
− | + | ||
− | def exo2( ): | + | def exo2( ): |
ans = raw_input( "Should we continue (Yes/No)? " ) | ans = raw_input( "Should we continue (Yes/No)? " ) | ||
ans = ans.lower() | ans = ans.lower() | ||
while not( ans in [ 'y', 'n', 'yes', 'no' ] ): | while not( ans in [ 'y', 'n', 'yes', 'no' ] ): | ||
ans = raw_input( "Invalid answer, please enter Yes or No: " ).lower() | ans = raw_input( "Invalid answer, please enter Yes or No: " ).lower() | ||
− | + | ||
return ( ans in [ 'y', 'yes' ] ) | return ( ans in [ 'y', 'yes' ] ) | ||
− | + | ||
− | def exo3( filename, key ): | + | def exo3( filename, key ): |
file = open( filename, 'r' ) | file = open( filename, 'r' ) | ||
count = 0 | count = 0 | ||
Line 65: | Line 77: | ||
count += 1 | count += 1 | ||
break | break | ||
− | + | ||
file.close() | file.close() | ||
− | + | ||
if count==0: | if count==0: | ||
print "Key not found" | print "Key not found" | ||
− | + | ||
− | def exo3b( filename, key ): | + | def exo3b( filename, key ): |
file = open( filename, 'r' ) | file = open( filename, 'r' ) | ||
count = 0 | count = 0 | ||
Line 80: | Line 92: | ||
return | return | ||
line = file.readline() | line = file.readline() | ||
− | + | ||
print "Key not found" | print "Key not found" | ||
− | + | ||
− | def exo4(): | + | def exo4(): |
sum = 0 | sum = 0 | ||
num = 400.0 | num = 400.0 | ||
Line 95: | Line 107: | ||
count += 1 | count += 1 | ||
print count, "iterations: sum = ", sum/100.0 | print count, "iterations: sum = ", sum/100.0 | ||
− | + | ||
− | def exo5(): | + | def exo5(): |
player1 = 0 | player1 = 0 | ||
player2 = 0 | player2 = 0 | ||
Line 105: | Line 117: | ||
while not ( play in [ "PP", "RR", "SS", "PR", "PS", "RP", "RS", "SP", "SR" ] ): | while not ( play in [ "PP", "RR", "SS", "PR", "PS", "RP", "RS", "SP", "SR" ] ): | ||
play = raw_input( "Invalid input: try again: " ).upper() | play = raw_input( "Invalid input: try again: " ).upper() | ||
− | + | ||
#--- check who's winning --- | #--- check who's winning --- | ||
if play in [ "PR", "RS", "SP" ]: | if play in [ "PR", "RS", "SP" ]: | ||
Line 112: | Line 124: | ||
player2 += 1 | player2 += 1 | ||
print "Player1: %d -- Player2: %d" % ( player1, player2 ) | print "Player1: %d -- Player2: %d" % ( player1, player2 ) | ||
− | + | ||
#--- print winner --- | #--- print winner --- | ||
if player1 > player2: | if player1 > player2: | ||
Line 119: | Line 131: | ||
print "Player 2 wins!" | print "Player 2 wins!" | ||
− | def exo6(): | + | def exo6(): |
c1 = 'a' | c1 = 'a' | ||
while c1 <= 'b': | while c1 <= 'b': | ||
Line 128: | Line 140: | ||
c2 = chr( ord( c2 ) + 1 ) | c2 = chr( ord( c2 ) + 1 ) | ||
c1 = chr( ord( c1 ) + 1 ) | c1 = chr( ord( c1 ) + 1 ) | ||
− | + | ||
− | def main(): | + | def main(): |
print "Exercise 1" | print "Exercise 1" | ||
print "exo1() returns", exo1() | print "exo1() returns", exo1() | ||
− | + | ||
print "Exercise 2" | print "Exercise 2" | ||
ans = exo2() | ans = exo2() | ||
print "exo2() returns", ans | print "exo2() returns", ans | ||
− | + | ||
exo3( raw_input( "file name? " ), raw_input( "keyword? " ) ) | exo3( raw_input( "file name? " ), raw_input( "keyword? " ) ) | ||
exo3b( raw_input( "file name? " ), raw_input( "keyword? " ) ) | exo3b( raw_input( "file name? " ), raw_input( "keyword? " ) ) | ||
− | + | ||
exo4() | exo4() | ||
− | + | ||
exo5() | exo5() | ||
− | + | ||
exo6() | exo6() | ||
− | + | ||
− | main() | + | main() |
− | |||
− | |||
− | |||
+ | |||
+ | </source> | ||
+ | </onlydft> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 13:00, 22 March 2015
Contents
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.
- Example:
Please enter a number greater than 0: -1 Invalid number. Please enter a number greater than 0: -10 Invalid number. Please enter a number greater than 0: 0 Invalid number. Please enter a number greater than 0: 10 (the function then returns 10 to the code section where it was called)
- 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 the string "<<<END>>>". The function receives the name of the file and returns the lines read.
- 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