Difference between revisions of "CSC111 Exercises with IF statements 2014"

From dftwiki3
Jump to: navigation, search
 
Line 4: Line 4:
 
__TOC__
 
__TOC__
 
<br />
 
<br />
 +
=Exercise Group 1=
 
==Source==
 
==Source==
 
<source lang="python">
 
<source lang="python">
Line 95: Line 96:
 
     print "f6 done!"
 
     print "f6 done!"
  
 +
f1()
 +
f2()
 +
f3()
 +
f4()
 +
f5()
 +
f6()
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Yes/No Answer=
 +
<br />
 +
 +
How would you write a code segment that would ask the user for a Yes/No answer, and print, say "<font color="magenta">Statement 1</font>" if the user enters "Y", "y", "yes" or "YES", and would print "<font color="magenta">Statement 2</font>" otherwise?
 +
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 09:14, 12 February 2014

--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?