CSC111 Exercises with If statements
--D. Thiebaut 13:55, 10 March 2010 (UTC)
# 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 "f2 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 "f2 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 "f2 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()