Difference between revisions of "CSC111 Games around Functions"

From dftwiki3
Jump to: navigation, search
(Game 4)
(Game 5)
 
Line 99: Line 99:
 
=Game 5=
 
=Game 5=
 
<br />
 
<br />
* Student 1 ( sentence )
+
* Student 1 ( sentence1, sentence2 )
** Get sentence
+
** Get sentence1 and sentence2
** Say "My T-shirt says"
+
** ask Student2 to say sentence1
** Say sentence
+
** ask Student3 to say sentence2
  
* Student 2 (sentence1, sentence 2 )
+
* Student 2 (sentence )
** get sentence 1
+
** get sentence  
** get sentence 2
+
** Say "Hear this:" and then say sentence
** Say sentence 1
 
** Call Student 1 and pass sentence 2
 
** Say "Thank you!"
 
  
* Student 3 (sentence 3 )
+
* Student 3 (sentence )
** get sentence 3
+
** get sentence  
** call student 2 and pass sentence3, and "Hello"
+
** Say "The news is: " and then say sentence
  
 
* main
 
* main
** Call Student 3 and pass "Caramba!"
+
** Call Student 1 and pass "Spring Break in two weeks!", and "Exam in a week and a half"  
 
<br />
 
<br />
 
<br />
 
<br />
Line 131: Line 128:
 
<br />
 
<br />
 
<br />
 
<br />
 +
<onlydft>
 +
=Solution Code=
 +
<source lang="python">
 +
# playingWithFunctions.py
 +
# D. Thiebaut  CSC111
 +
#
 +
 +
#--- first set of exercises ---
 +
"""
 +
def hello():
 +
    print( "hello" )
 +
 +
def helloBox():
 +
    print( "+-----+" )
 +
    print( "|hello|" )
 +
    print( "+-----+" )
 +
    print()
 +
   
 +
hello()
 +
helloBox()
 +
for i in range( 3 ):
 +
    helloBox()
 +
"""
 +
 +
#--- Game 1 implementation ---
 +
def student1():
 +
    print( "Banana!" )
 +
 +
def student2():
 +
    print( "Orange!" )
 +
 +
def student3():
 +
    print( "Peanut Butter!" )
 +
 +
"""
 +
student1()
 +
student2()
 +
student3()
 +
"""
 +
 +
#--- Game 2 implementation ---
 +
"""
 +
for i in range( 3 ):
 +
    student1()
 +
   
 +
student2()
 +
 +
for i in range( 2 ):
 +
    student3()
 +
"""
 +
 +
#--- Game 3 implementation ---
 +
def student3Game3():
 +
    student1()
 +
    print( "Si!" )
 +
"""
 +
student2()
 +
student1()
 +
student3Game3()
 +
"""
 +
 +
#--- Game 4 implementation ---
 +
def student1Game4( sentence ):
 +
    print( sentence )
 +
 +
def student2Game4( sentence ):
 +
    print( "My T-shirt says", sentence )
 +
 +
def student3Game4( sentence ):
 +
    print( "And now listen to this:", sentence )
 +
 +
student1Game4( "I am tired" )
 +
student2Game4( "I live for chocolate!" )
 +
student3Game4( "Almost Spring Break!" )
 +
 +
#--- Game 5 implementation ---
 +
def student1Game5( sentence1, sentence2 ):
 +
    student2Game5( sentence1 )
 +
    student3Game5( sentence2 )
 +
 +
def student2Game5( sentence ):
 +
    print( "Hear this:", sentence )
 +
 +
def student3Game5( sentence ):
 +
    print( "The news is:", sentence )
 +
 +
string1 = "Spring break in two weeks"
 +
string2 = "Exam in a week and a half"
 +
student1Game5( string1, string2 )
 +
 +
 +
</source>
 +
</onlydft>
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Python]][[Category:Exercises]]
 
[[Category:CSC111]][[Category:Python]][[Category:Exercises]]

Latest revision as of 11:00, 3 March 2014

--D. Thiebaut 09:47, 20 October 2011 (EDT)




Each game below can be played in class just to get the idea of what goes on. Think of the game as a play. Students are actors and have lines to say. When the director points to an actor, the actor says her line. Once a game is played in class and fully understood, translate it into python, using functions.


Game 1


  • Student 1
    • Say "Banana!"
  • Student 2
    • Say "Orange!"
  • Student 3
    • Say "Peanut Butter!"
  • Main
    • Call Student1
    • Call Student2
    • Call Student3






Game 2



* Student 1

    • Say "Banana!"
  • Student 2
    • Say "Orange!"
  • Student 3
    • Say "Peanut Butter!"
  • Main
    • repeat 3 times:
      • Call Student1
    • Call Student2
    • repeat 2 times:
      • Call Student3






Game 3


  • Student 1
    • Say "Caramba!"
  • Student 2
    • Say "No, No, No!"
  • Student 3
    • Call Student 1
    • Say "Si!"
  • Main
    • Call Student 2
    • Call Student 1
    • Call Student 3





Game 4


  • Student 1 ( sentence )
    • Get sentence
    • Say the sentence
  • Student 2 ( sentence )
    • Get sentence
    • Say "My T-Shirt says: "
    • Say the sentence
  • Student 3 ( sentence )
    • Get sentence
    • Say "And now listen to this: "
    • Call student 1 and pass her sentence
  • Main
    • Call Student 1 and pass "I am tired"
    • Call Student 2 and pass "I live for Chocolate"
    • Call Student 3 and pass "Almost Spring Break!"






Game 5


  • Student 1 ( sentence1, sentence2 )
    • Get sentence1 and sentence2
    • ask Student2 to say sentence1
    • ask Student3 to say sentence2
  • Student 2 (sentence )
    • get sentence
    • Say "Hear this:" and then say sentence
  • Student 3 (sentence )
    • get sentence
    • Say "The news is: " and then say sentence
  • main
    • Call Student 1 and pass "Spring Break in two weeks!", and "Exam in a week and a half"
















...