Difference between revisions of "CSC111 Exercises with If statements"

From dftwiki3
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
 
__TOC__
 
__TOC__
 +
<br />
 +
=If Statements in Python V. 2=
 
<br />
 
<br />
 
==Source==
 
==Source==
Line 64: Line 66:
 
             print "statement 3"
 
             print "statement 3"
 
         print "statement 4"
 
         print "statement 4"
     print "f2 done!"
+
     print "f4 done!"
  
 
def f5():
 
def f5():
Line 78: Line 80:
 
             print "statement 3"
 
             print "statement 3"
 
         print "statement 4"
 
         print "statement 4"
     print "f2 done!"
+
     print "f5 done!"
  
 
def f6():
 
def f6():
Line 92: Line 94:
 
             print "statement 3"
 
             print "statement 3"
 
         print "statement 4"
 
         print "statement 4"
     print "f2 done!"
+
     print "f6 done!"
 
      
 
      
 
def f7():
 
def f7():
Line 161: Line 163:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
==Output==
 
==Output==
 
<code><pre>
 
<code><pre>
Line 170: Line 173:
 
f1 done!
 
f1 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 2
 
Which function do you want to run? 2
Line 176: Line 181:
 
f2 done!
 
f2 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 3
 
Which function do you want to run? 3
Line 182: Line 189:
 
f3 done!
 
f3 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 4
 
Which function do you want to run? 4
Line 187: Line 196:
 
statement 3
 
statement 3
 
statement 4
 
statement 4
f2 done!
+
f4 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 5
 
Which function do you want to run? 5
Line 194: Line 205:
 
statement 3
 
statement 3
 
statement 4
 
statement 4
f2 done!
+
f5 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 6
 
Which function do you want to run? 6
Line 201: Line 214:
 
statement 3
 
statement 3
 
statement 4
 
statement 4
f2 done!
+
f6 done!
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 7
 
Which function do you want to run? 7
Line 213: Line 228:
 
rejects =  [('f', 10), ('d', 0), ('c', 100)]
 
rejects =  [('f', 10), ('d', 0), ('c', 100)]
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 8
 
Which function do you want to run? 8
Line 223: Line 240:
 
rejects =  [('a', 3), ('f', 10), ('d', 0), ('c', 100)]
 
rejects =  [('a', 3), ('f', 10), ('d', 0), ('c', 100)]
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 9
 
Which function do you want to run? 9
Line 233: Line 252:
 
rejects =  [('a', 3), ('z', 5), ('f', 10), ('d', 0)]
 
rejects =  [('a', 3), ('z', 5), ('f', 10), ('d', 0)]
 
--------------------------------------------------
 
--------------------------------------------------
 +
 +
 
[111c@beowulf ~/temp]$ python if_tests.py
 
[111c@beowulf ~/temp]$ python if_tests.py
 
Which function do you want to run? 10
 
Which function do you want to run? 10

Latest revision as of 18:14, 28 February 2015

--D. Thiebaut 13:55, 10 March 2010 (UTC)



If Statements in Python V. 2


Source

# if_tests.py
# D. Thiebaut
# A collection of if/else statements.
# Figure out what gets printed!

def f1():
    a = 3
    b = 5
    c = 10
    if a < b:
        print "statement 1"
    else:
        if a < c:
            print "statement 2"
        else:
            if b < c:
                print "statement 3"
    print "f1 done!"

def f2():
    a = 30
    b = 5
    c = 10
    if a < b:
        print "statement 1"
    else:
        if a < c:
            print "statement 2"
        else:
            if b < c:
                print "statement 3"
    print "f2 done!"

def f3():
    a = 30
    b = 50
    c = 10
    if a < b:
        print "statement 1"
    else:
        if a < c:
            print "statement 2"
        else:
            if b < c:
                print "statement 3"
    print "f3 done!"

def f4():
    a = 30
    b = 5
    c = 10
    if a < b < c:
        print "statement 1"
    else:
        if a < c < b:
            print "statement 2"
        else:
            print "statement 3"
        print "statement 4"
    print "f4 done!"

def f5():
    a = 30
    b = 50
    c = 10
    if a < b < c:
        print "statement 1"
    else:
        if a < c < b:
            print "statement 2"
        else:
            print "statement 3"
        print "statement 4"
    print "f5 done!"

def f6():
    a = 3
    b = 5
    c = 1
    if a < b < c:
        print "statement 1"
    else:
        if a < c < b:
            print "statement 2"
        else:
            print "statement 3"
        print "statement 4"
    print "f6 done!"
    
def f7():
    L = ( ("a", 3), ("z", 5 ), ("f", 10), ("d", 0), ("c", 100) )
    newL = []
    rejects = []
    for name, val in L:
        if 0 < val < 10:
            newL.append( (name, val) )
        else:
            rejects.append( (name, val ) )
        print "newL = ", newL
    print "rejects = ", rejects

def f8():
    L = ( ("a", 3), ("z", 5 ), ("f", 10), ("d", 0), ("c", 100) )
    newL = []
    rejects = []
    for name, val in L:
        if val <= 10 and name > "f":
            newL.append( (name, val) )
        else:
            rejects.append( (name, val ) )
        print "newL = ", newL
    print "rejects = ", rejects

def f9():
    L = ( ("a", 3), ("z", 5 ), ("f", 10), ("d", 0), ("c", 100) )
    newL = []
    rejects = []
    for name, val in L:
        if not ( val <= 10 or  name > "f" ):
            newL.append( (name, val) )
        else:
            rejects.append( (name, val ) )
        print "newL = ", newL
    print "rejects = ", rejects
    
def main():
    functionId = input( "Which function do you want to run? " )
    print "-"*50

    if functionId == 1:
        f1()
    elif functionId == 2:
        f2()
    elif functionId == 3:
        f3()
    elif functionId == 4:
        f4()
    elif functionId == 5:
        f5()
    elif functionId == 6:
        f6()
    elif functionId == 7:
        f7()
    elif functionId == 8:
        f8()
    elif functionId == 9:
        f9()
    else:
        print "\n\n*** Invalid number! ***\n\n"
    print "-"*50
        
main()



Output


python if_tests.py
Which function do you want to run? 1
--------------------------------------------------
statement 1
f1 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 2
--------------------------------------------------
statement 3
f2 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 3
--------------------------------------------------
statement 1
f3 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 4
--------------------------------------------------
statement 3
statement 4
f4 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 5
--------------------------------------------------
statement 3
statement 4
f5 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 6
--------------------------------------------------
statement 3
statement 4
f6 done!
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 7
--------------------------------------------------
newL =  [('a', 3)]
newL =  [('a', 3), ('z', 5)]
newL =  [('a', 3), ('z', 5)]
newL =  [('a', 3), ('z', 5)]
newL =  [('a', 3), ('z', 5)]
rejects =  [('f', 10), ('d', 0), ('c', 100)]
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 8
--------------------------------------------------
newL =  []
newL =  [('z', 5)]
newL =  [('z', 5)]
newL =  [('z', 5)]
newL =  [('z', 5)]
rejects =  [('a', 3), ('f', 10), ('d', 0), ('c', 100)]
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 9
--------------------------------------------------
newL =  []
newL =  []
newL =  []
newL =  []
newL =  [('c', 100)]
rejects =  [('a', 3), ('z', 5), ('f', 10), ('d', 0)]
--------------------------------------------------


[111c@beowulf ~/temp]$ python if_tests.py
Which function do you want to run? 10
--------------------------------------------------


*** Invalid number! ***


--------------------------------------------------