Difference between revisions of "CSC111 Lab 3 2014"

From dftwiki3
Jump to: navigation, search
(If Statements: Loosening Up)
 
(17 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
__TOC__
 
__TOC__
 
<br /><br /><br />
 
<br /><br /><br />
 
+
<bluebox>
 +
This lab deals with if-statements and will have you practice writing tests, and better understanding the logic involved in tests.
 +
</bluebox>
 +
<br />
 +
<br />
 +
=Video Intro=
 +
<br />
 +
<center><videoflash>1FomulmEjuk</videoflash></center>
 +
<br />
 +
<br />
 
=If Statements: Loosening Up=
 
=If Statements: Loosening Up=
 
+
<br />
 
Using the code snippet shown below, change the ''boolean expression'' inside the '''if-statement''' so that the program behaves the required way:
 
Using the code snippet shown below, change the ''boolean expression'' inside the '''if-statement''' so that the program behaves the required way:
 
<br />
 
<br />
Line 33: Line 42:
 
<br />
 
<br />
 
; Test Case #1
 
; Test Case #1
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is greater than 10.  Run your program and verify that it works for values equal to 0 and values not equal to 0.
+
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is greater than 10.  Run your program and verify that it works for values less than 10 and values greater than 10.  10 should be reported invalid.
 
<br />
 
<br />
 
; Test Case #2
 
; Test Case #2
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is greater between the values of 10 and 20, excluded.  The values 10 and 20 are not considered valid in this test case.
+
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is   between the values of 10 and 20, excluded.  The values 10 and 20 are not considered valid in this test case.
 
<br />
 
<br />
 
; Test Case #3
 
; Test Case #3
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is greater between the values of 10 and 20, '''included'''.  The values 10 and 20 '''are''' considered valid in this test case.
+
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is   between the values of 10 and 20, '''included'''.  The values 10 and 20 '''are''' considered valid in this test case.
 
<br />
 
<br />
 
; Test Case #4
 
; Test Case #4
Line 50: Line 59:
 
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is divisible by 2 and between 0 and 100,  included.  Verify that your program correctly accepts 0, 10, 90, 100, but rejects  -10, 99, or 101.
 
: modify the boolean expression so that the program outputs '''<font color="magenta">valid input</font>''' '''only if''' x is divisible by 2 and between 0 and 100,  included.  Verify that your program correctly accepts 0, 10, 90, 100, but rejects  -10, 99, or 101.
 
<br />
 
<br />
 +
 +
=Finding the Largest of 3 Items=
 +
 +
<br />
 +
Write a program that prompts the user for 3 integers and outputs the largest of the 3.  There are many different ways to do this with '''if statements'''. 
 +
 +
First try to do this on your own without looking at the solution we found in class.  After trying for a few minutes and not getting anywhere, you may quickly read the solution we wrote in class, and then, without looking at it again, try to recreate it in Idle.
 +
 +
Make sure your program works if the user enter the same 3 numbers!
 +
<br />
 +
 
=Testing Strings=
 
=Testing Strings=
 
<br />
 
<br />
Line 82: Line 102:
 
   
 
   
 
<br />
 
<br />
 +
If you still are clueless about the way '''find()''' works, try this:
 +
 +
>>> name3 = "abcdefghijklmnopqrstuvwxyz"
 +
>>> name3.find( 'a' )
 +
 +
>>> name3.find( 'b' )
 +
 +
>>> name3.find( 'c' )
 +
 +
>>> name3.find( 'z' )
 +
 +
>>> name3.find( '@' )
 +
  
 
Now try this simple program:
 
Now try this simple program:
Line 142: Line 175:
  
 
<br />
 
<br />
 +
 
=Rock, Paper, Scissors=
 
=Rock, Paper, Scissors=
 
<br />
 
<br />
 +
[[Image:RockPaperScissors.jpg|right|150px]]
 
The code below has some advanced features we haven't seen yet.  Don't worry about the new statements.  Running the program will help you figure out what their purpose is in the program.   
 
The code below has some advanced features we haven't seen yet.  Don't worry about the new statements.  Running the program will help you figure out what their purpose is in the program.   
  
Line 155: Line 190:
  
 
computer = choice( OPTIONS )
 
computer = choice( OPTIONS )
#human = input( "What is your play? " ).upper()
+
 
 
 
print( "The compute has chosen", computer )
 
print( "The compute has chosen", computer )
  
 +
#human = input( "What is your play? " ).upper()
 
   
 
   
 
</source>
 
</source>
 
<br />
 
<br />
  
Uncomment the '''input()''' statement and add make the program compare the user's choice to the computer choice and  announce the winner.  If you need the rules for this game, check this URL: [http://en.wikipedia.org/wiki/Rock-paper-scissors http://en.wikipedia.org/wiki/Rock-paper-scissors].
+
Uncomment the '''input()''' statement and add make the program compare the user's choice to the computer choice and  announce the winner.   
 +
 
 +
If you need the rules for this game, check this URL: [http://en.wikipedia.org/wiki/Rock-paper-scissors http://en.wikipedia.org/wiki/Rock-paper-scissors].
  
  
Line 175: Line 212:
 
[[Image:QuestionMark3.jpg|right|120px]]
 
[[Image:QuestionMark3.jpg|right|120px]]
 
<br />
 
<br />
Modify your program so that it plays 3 rounds of the game.  For each round it will generate a new computer play using
+
Modify your program so that it plays 3 rounds of the game.  For each round it will generate a new random choice for the computer with this line:
  
 
  computer = choice( OPTIONS )
 
  computer = choice( OPTIONS )
  
and prompt the user for a new selection.  After every round the computer will announce the winner.  DO NOT USE LOOPS.  Just three copies of the same lines of code is fine!
+
after which it will prompt the user for a new selection.   
 +
 
 +
After every round the computer will announce the winner.  DO NOT USE LOOPS.  Just three copies of the same lines of code is fine!
 
<br />
 
<br />
 
<br /><br />
 
<br /><br />
Line 185: Line 224:
 
|-
 
|-
 
|
 
|
==Challenge #4: Keeping scores==
+
 
 +
==Challenge #4: Adding some Robustness (Optional)==
 
|}
 
|}
 
[[Image:QuestionMark4.jpg|right|120px]]
 
[[Image:QuestionMark4.jpg|right|120px]]
 +
<br />
 +
Your program (very likely) will work if the user enters her selection using uppercase letters, but not if she enters  lowercase
 +
letters.
 +
 +
Modify your program from Challenge 4 so that it will work whether the user
 +
* enters lower- or upper-case letters, and
 +
* adds extra spaces before or after her letter.
 +
 +
<br />
 +
<br />
 +
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 +
==Challenge #5: Keeping scores (Optional)==
 +
|}
 +
[[Image:QuestionMark5.jpg|right|120px]]
 
<br />
 
<br />
 
Modify your program from Challenge 3 and make it keep tack of the score.  At the end of the 3 rounds the program will announce who won the 3 rounds.
 
Modify your program from Challenge 3 and make it keep tack of the score.  At the end of the 3 rounds the program will announce who won the 3 rounds.
  
 
This is tricky.  Try to figure it out on your own and don't give up for 5 minutes.  Try hard.  If after that long you still can't figure it out, ask the TAs or your instructor for hints...
 
This is tricky.  Try to figure it out on your own and don't give up for 5 minutes.  Try hard.  If after that long you still can't figure it out, ask the TAs or your instructor for hints...
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
 +
=Submission=
 +
<br />
 +
* Store the last program that you were able to run without errors, or the whole set of programs if you wrote all of them in one file) in a file called '''lab3.py'''.
 +
 +
* Write down your name at the top of the program, in a comment, and add your 2-letter Id (the list of 2-letter Ids is available [[CSC111_2-Letter_Ids_2014| here]]).  Follow this example
 +
 +
# lab3.py
 +
# Minnie Mouse  (cc)
 +
# Solution program for ...
 +
 +
* If you worked on this lab with a partner, add the two names in the program header.
 +
 +
# lab3.py
 +
# Mickey Mouse  (aa)
 +
# Minnie Mouse (cc)
 +
# Solution program for ...
 +
 +
* Submit your program [http://cs.smith.edu/~thiebaut/111b/submitL3.php here].
 +
 +
<br />
 +
<br />
 +
<br />
 +
That's it for this lab!
 +
<br />
 +
<br />
 +
<br />
 +
 +
[[Category:CSC111]][[Category:Python]][[Category:Labs]]

Latest revision as of 11:22, 13 February 2014

--D. Thiebaut (talk) 07:22, 12 February 2014 (EST)





This lab deals with if-statements and will have you practice writing tests, and better understanding the logic involved in tests.



Video Intro




If Statements: Loosening Up


Using the code snippet shown below, change the boolean expression inside the if-statement so that the program behaves the required way:

# Validity test example
x = int( input( "Please enter an integer: " ) )
if    ...      :
    print( "valid input" )
else:
    print( "invalid input" )


Example: put a boolean expression such that the program will print valid input if x is an integer different from 0.

In this case you would change the program to:

# Validity test example
x = int( input( "Please enter an integer: " ) )
if    x != 0     :
    print( "valid input" )
else:
    print( "invalid input" )


Test Case #1
modify the boolean expression so that the program outputs valid input only if x is greater than 10. Run your program and verify that it works for values less than 10 and values greater than 10. 10 should be reported invalid.


Test Case #2
modify the boolean expression so that the program outputs valid input only if x is between the values of 10 and 20, excluded. The values 10 and 20 are not considered valid in this test case.


Test Case #3
modify the boolean expression so that the program outputs valid input only if x is between the values of 10 and 20, included. The values 10 and 20 are considered valid in this test case.


Test Case #4
modify the boolean expression so that the program outputs valid input only if x is an even number only.


Test Case #5
modify the boolean expression so that the program outputs valid input only if x is divisible by 2 and divisible by 5. Test your program with numbers such as 18, 20, 22, -10, etc.


Test Case #6
modify the boolean expression so that the program outputs valid input only if x is divisible by 2 and between 0 and 100, included. Verify that your program correctly accepts 0, 10, 90, 100, but rejects -10, 99, or 101.


Finding the Largest of 3 Items


Write a program that prompts the user for 3 integers and outputs the largest of the 3. There are many different ways to do this with if statements.

First try to do this on your own without looking at the solution we found in class. After trying for a few minutes and not getting anywhere, you may quickly read the solution we wrote in class, and then, without looking at it again, try to recreate it in Idle.

Make sure your program works if the user enter the same 3 numbers!

Testing Strings


We saw that strings are objects, and as such have methods that allow us to either generate new strings, or somehow explore what is inside the string.

Try the commands below and see how the string find( ) method works:

>>> name = "Santa Claus"
>>> name.find( 'a' )

>>> name.find( 'c' )

>>> name. find( 'santa' )

>>> name.find( 'SANTA' )

>>> name.find( 'a C' )

>>> name.find( 'Claus' )

>>> name.find( 'Mrs Claus' )

>>> name2 = name.lower()
>>> name2.find( 'Santa' )

>>> name2.find( 'santa' )

>>> name2 = name.upper()
>>> name2.find( 'santa' )

>>> name2.find( 'SANTA' )


If you still are clueless about the way find() works, try this:

>>> name3 = "abcdefghijklmnopqrstuvwxyz"
>>> name3.find( 'a' )

>>> name3.find( 'b' )

>>> name3.find( 'c' )

>>> name3.find( 'z' )

>>> name3.find( '@' )

Now try this simple program:


name = input( "Hello!  What is your name? " )

if name.find( "Santa" ) != -1:
    print( "Ho, Ho, Ho Santa Claus!" )
else:
    print( "Hello there, " + name + "!" )


Challenge #1: Adding robustness

QuestionMark1.jpg


Run the program again and try the following names when the program prompts you:

  • Santa
  • Nicholas
  • Mrs Claus
  • SANTA
  • santa

You will have noticed that the program does not always recognize Santa because of the uppercase/lowercase spelling. Modify the program so that it recognizes Santa and greets him properly whether it is spelled SANTA, santa, or even SanTA.

Challenge #2: Mother and Father

QuestionMark2.jpg


After a few more lectures we'll write a program that emulates a therapist who dialogs with the computer user. A useful test for this program is to recognize if a phrase entered by the user contains keywords, such as mother, father, brother or sister.

Write a program that prompts the user for a sentence. The prompt given by the program could be "How was your day?". Once the program has the user input in a string, make it test the string and if the user entered the words mother or father, whether in uppercase or lowercase, the program will indicate that it has detected a family topic, else it will simply prompt something vague.
Here's an example of how your program should work (the user input is in boldface):

How was your day? I had a great day so far!

Glad to hear that!

And another example:

How was your day? I just had a phone call from my mother

Should we talk about your family?


Rock, Paper, Scissors


RockPaperScissors.jpg

The code below has some advanced features we haven't seen yet. Don't worry about the new statements. Running the program will help you figure out what their purpose is in the program.

Copy the code below into Idle and run it a few times:


from random import choice

OPTIONS = ['R', 'P', 'S' ]

computer = choice( OPTIONS )

print( "The compute has chosen", computer )

#human = input( "What is your play? " ).upper()


Uncomment the input() statement and add make the program compare the user's choice to the computer choice and announce the winner.

If you need the rules for this game, check this URL: http://en.wikipedia.org/wiki/Rock-paper-scissors.





Challenge #3: Playing 3 rounds

QuestionMark3.jpg


Modify your program so that it plays 3 rounds of the game. For each round it will generate a new random choice for the computer with this line:

computer = choice( OPTIONS )

after which it will prompt the user for a new selection.

After every round the computer will announce the winner. DO NOT USE LOOPS. Just three copies of the same lines of code is fine!


Challenge #4: Adding some Robustness (Optional)

QuestionMark4.jpg


Your program (very likely) will work if the user enters her selection using uppercase letters, but not if she enters lowercase letters.

Modify your program from Challenge 4 so that it will work whether the user

  • enters lower- or upper-case letters, and
  • adds extra spaces before or after her letter.



Challenge #5: Keeping scores (Optional)

QuestionMark5.jpg


Modify your program from Challenge 3 and make it keep tack of the score. At the end of the 3 rounds the program will announce who won the 3 rounds.

This is tricky. Try to figure it out on your own and don't give up for 5 minutes. Try hard. If after that long you still can't figure it out, ask the TAs or your instructor for hints...





Submission


  • Store the last program that you were able to run without errors, or the whole set of programs if you wrote all of them in one file) in a file called lab3.py.
  • Write down your name at the top of the program, in a comment, and add your 2-letter Id (the list of 2-letter Ids is available here). Follow this example
# lab3.py
# Minnie Mouse  (cc)
# Solution program for ...
  • If you worked on this lab with a partner, add the two names in the program header.
# lab3.py
# Mickey Mouse  (aa)
# Minnie Mouse (cc)
# Solution program for ...
  • Submit your program here.




That's it for this lab!