Exercises on break, continue, and in statements

From dftwiki3
Revision as of 08:22, 25 October 2011 by Thiebaut (talk | contribs) (Exercise 4)
Jump to: navigation, search

--D. Thiebaut 07:58, 25 October 2011 (EDT)


Exercise 1

  • Given the list
L = [ 1, 10, 20, 5, 100, 110, 21, 500 ]
How can we use Python to detect if there exists a number in L that is larger than, say, 50, and record that value in a variable?

Exercise 2

  • Ask the user to enter an unspecified number of positive integers, add them to a list, and stop when the user enters -1. At most 100 integers can be entered.

Exercise 3

  • Given a list L = [ 1, 10, 20, 5, 100, 110, 21, 500 ], create a new list of the even numbers contained in L.

Exercise 4

  • Ask the user for a name and indicate whether this name is one of the 7 dwarves from Snow White.
seven = [  "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ] 





A program to get started...


# program for exercises on
# break, continue and in
#

def exercise1( L ):

    print( "exercise 1" )


def exercise2():

    print( "exercise 2" )


def exercise3( L ):

    print( "exercise 3" )


def exercise4( seven )

    print( "exercise 4" )


def main():
    L = [ 1, 10, 20, 5, 100, 110, 21, 500 ]
    seven = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]
    
    exercise1( L )

    exercise2()

    exercise3()

    exercise4( seven )

main()