CSI Lab 11

Tuesday, April 7th


Doing some testing and introduction to Strings


Introduction

While I will likely let you go back and work with a partner again, THIS week I would like you to work as an individual.

This week's lab is a relatively small series of activities that have you do a little bit of testing, use some conditionals that are a bit more advanced than things we have done, and introduce you to Strings.


Activity A : Doing some testing

Create a method called letterGrade().  Letter grade should take in a number which represents a score on an exam. 

The method letterGrade() should expect an int as a score and print an appropriate message based on the score.  Something like

 >>> letterGrade(95)
  A score of 95 is a A!
 >>> letterGrade(82)
  A score of 82 is a B!
 >>> letterGrade(79.4)
  A score of 79.4 is a C!
  >>> letterGrade(105)
  A score of 105 is impossible.
...
  

Write this method using a series of IF statements. Test it thoroughly to make sure it conforms to the directions given above.

[SIG1] When you feel you have letterGrade() working, show this to a TA.


Activity B : Introduction to Strings

Yesterday in class we spent a little bit of time looking at String as a data structure and some of the initial things you can do to the String. Let's review:

At the command prompt type (yes, use the strange caps)

>>> greet = "hello dr. Schafer"
[Q1] What happens when you invoke each of these commands:  (be careful with the last one)
Command Results
print len(greet)  
print greet[1]                                                                 
print greet[8]  
print greet[2:7]  
print greet[:5]  
print greet[9:]  
print greet[4]+greet[3]+greet[6]  
print greet[0]*3  
print (greet[0]+greet[4:6])*3  

 

[Q2] What commands would you type to get the following results

Command Results
                                                                             a                 
  the LAST r
  the last letter (suppose you don't know how what the message is)
  o dr. Sch
  dr.dr.dr.dr.SchSch
  car

 

 

[Q3] What happens when you invoke each of these commands: 

Command Results
print greet[-1]                                                                 
print greet[-4]  
print greet[-11]  
print greet[-7:-3]  

 

[Q4]  What is happening when you use negative numbers in the indexing/slice command?  (By the way, this isn't something that you would do often, but it IS something you can do)

 

 

 


Activity C : Let's do something useful

Suppose that every UNI email address was generated from the following formula:

The first 5 characters of your last name followed by the first initial of your first name followed by @uni.edu

For example:

Write this method and test that it works.

 


Activity D : Cleaning that up a bit

I see another problem with

MOST email addresses are case-insensitive and as such are reported as all lower-case letters.

Rather than "SchafB@uni.edu" we would return "schafb@uni.edu"

How can we do that?

It it isn't still in your system type in (again, with the strange caps)

>>> greet = "hello dr. Schafer"

Now import the string package which contains some common string commands:

>>> import string

[Q5] What happens when you invoke each of these commands: 

Command Results
print string.capitalize(greet)  
print string.capwords(greet)                                                                 
print string.lower(greet)  
print string.upper(greet)  
print string.count(greet,"c")  
print string.count(greet,"r")  
print string.find(greet,"r")  
print string.rfind(greet,"r")  
print string.split(greet)  

 

Use what you just observed to clean up and report the email addresses in a more "normal" format.

[SIG2]  Show this to a TA when you get the generateEmailAddress() method working.

 


Activity E : Finishing up pennyMath()

Yesterday at the end of class we were dealing with pennyMath and we had the code to this point

def pennyMath(string):
   value = 0
   for num in range(len(string)):
      letter=string[num]
      value = value + ord(letter) - 96
   print string+" would cost you "+str(value)

 

Finish this code so that it works with both upper and lowercase letters while ignoring non-alpha characters.

[SIG3]  Show pennyMath() to a TA when you get it working