Difference between revisions of "CSC111 Lab 3 2014"
(→If Statements: Loosening Up) |
(→Testing Strings) |
||
Line 111: | Line 111: | ||
* santa | * santa | ||
− | You will have noticed that the program does not always recognize '''Santa''' because of the | + | 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'''. | Modify the program so that it recognizes '''Santa''' and greets him properly whether it is spelled '''SANTA''', '''santa''', or even '''SanTA'''. | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | |||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge #2: Mother and Father== | ||
+ | |} | ||
+ | [[Image:QuestionMark2.jpg|right|120px]] | ||
+ | <br /> | ||
+ | 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. | ||
+ | <br /> | ||
+ | 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? |
Revision as of 09:02, 12 February 2014
--D. Thiebaut (talk) 07:22, 12 February 2014 (EST)
Contents
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 equal to 0 and values not equal to 0.
- Test Case #2
- modify the boolean expression so that the program outputs valid input 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.
- Test Case #3
- modify the boolean expression so that the program outputs valid input 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.
- 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.
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' )
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 |
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 |
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?