Difference between revisions of "CSC111 Sentinel Example 1"

From dftwiki3
Jump to: navigation, search
(Example 2)
(Working From the Command Line)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:36, 20 February 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:36, 20 February 2014 (EST)
 
----
 
----
 +
<br />
 +
__TOC__
 +
<br />
 +
<br />
 
=Example 1=
 
=Example 1=
 
<br />
 
<br />
Line 60: Line 64:
  
 
   
 
   
 +
==Working From the Command Line==
  
 +
* Processing the poems of [http://cs.smith.edu/~thiebaut/gutenberg/12241.txt Emily Dickinson]
 +
* Two videos
 +
[[Image:videoLogo.png| 50px | right | link = http://www.youtube.com/watch?v=6x2qtLrQdqU]]
 +
[[Image:videoLogo.png| 50px | right | link = http://www.youtube.com/watch?v=ELRMtELi0zE]]
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 
<br />
 
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Exercises]]

Latest revision as of 16:56, 20 February 2014

--D. Thiebaut (talk) 15:36, 20 February 2014 (EST)





Example 1


# sumNum.py
# D. Thiebaut
#
# Computes the sum of positive numbers entered at the keyboard 
# until a negative number is entered is entered

sum = 0
x     = 0

while x >= 0:
    x = int( input( "> " ) )
    if x > 0:
        sum = sum + x

print( "sum = ", sum )
Question
What if the user wants to enter the sum of positive and negative numbers?


Example 2


# countKeywords.py
# D. Thiebaut
# counts number of lines where two different keywords may occur

KEY1 = "MOTHER"
KEY2 = "FATHER"
SENTINEL = "ZZZENDZZZ"

# counters
key1Count = 0
key2Count = 0
timeToStop= False

while timeToStop == False:
    line = input().upper()
 
    if line.find( SENTINEL ) != -1:
       timeToStop = True
    else:
       if line.find( KEY1 ) != -1:
          key1Count = key1Count + 1
       if line.find( KEY2 ) != -1:
          key2Count = key2Count + 1

print( key1Count, "line(s) with keyword", KEY1 )
print( key2Count, "line(s) with keyword", KEY2 )


Working From the Command Line

link = http://www.youtube.com/watch?v=6x2qtLrQdqU
link = http://www.youtube.com/watch?v=ELRMtELi0zE