CSC111 Exercises with IF statements 2014

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 09:10, 12 February 2014 (EST)



Exercise Group 1

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!"

f1()
f2()
f3()
f4()
f5()
f6()


Yes/No Answer


How would you write a code segment that would ask the user for a Yes/No answer, and print, say "Statement 1" if the user enters "Y", "y", "yes" or "YES", and would print "Statement 2" otherwise?