Exercises on break, continue, and in statements

From dftwiki3
Revision as of 17:03, 26 October 2011 by Thiebaut (talk | contribs) (Exercise 4)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 find the first number that is greater 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. Give the user 10 trials. Make your program output a "sorry you lose" message at the end if the user never got the right answer.
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( L )

    exercise4( seven )

main()




...