word jumble game in python

Upload: michelle-hsieh

Post on 07-Aug-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 Word Jumble Game in Python

    1/48

    Guide to Programming with

    Python

    Chapter Four for  Loops, Strings, and Tuples: The Word

    Jumble Game

  • 8/20/2019 Word Jumble Game in Python

    2/48

    Guide to Programming with Python 2

    Objectives

    • Construct for loops to move through a sequence

    • Use the range() function to create a sequence of

    integers

    • reat strings as sequences

    • Use tuples to harness the power of sequences

    • Use sequence functions and operators

    • !nde" and slice sequences

  • 8/20/2019 Word Jumble Game in Python

    3/48

    Guide to Programming with Python #

    he $ord %umble Game

    &igure '()* +ample run of the $ord %umble game

    his jumble loo,s -difficult(.

  • 8/20/2019 Word Jumble Game in Python

    4/48

    Guide to Programming with Python '

    Using for /oops

    • for loop

     0 /i,e while loop1 repeats a loop body

     0 Unli,e while loop1 doesnt repeat based on condition

     0 3epeats loop body for each element in a sequence

     0 4nds when it reaches end of the sequence

     0 e(g(1 go through sequence of game titles and print

    each

  • 8/20/2019 Word Jumble Game in Python

    5/48

    Guide to Programming with Python 5

    he /oopy +tring Program

    &igure '(2* +ample run of the /oopy +tring program

     6 for loop goes through a word1 one character at a time(

  • 8/20/2019 Word Jumble Game in Python

    6/48

    Guide to Programming with Python 7

    Understanding for /oops

    • Sequence: 6n ordered list of elements

    • Element: 6 single item in a sequence

    • Iterate: o move through a sequence1 in order 

    • /ist of your top8ten movies

     0 6 sequence

     0 4ach element is a movie title

     0 o iterate over would be to go through each title1 inorder 

  • 8/20/2019 Word Jumble Game in Python

    7/48

    Guide to Programming with Python 9

    • for loop iterates over a sequence: performs loop

    body for each element

    • ;uring each iteration1 loop variable gets ne"t

    element• !n loop body1 something usually done with loop

    variable

    Understanding for /oops

  • 8/20/2019 Word Jumble Game in Python

    8/48

    Guide to Programming with Python >

    Understanding for /oops

  • 8/20/2019 Word Jumble Game in Python

    9/48

    Guide to Programming with Python ?

    Counting with a for /oop

    • Can use for loop to count

    • Can use in combination with range() function

  • 8/20/2019 Word Jumble Game in Python

    10/48

    Guide to Programming with Python )@

    he Counter Program

    &igure '(#* +ample run of the Counter program

    Using a for loop1 counts forward1 by fives1 and bac,ward(

  • 8/20/2019 Word Jumble Game in Python

    11/48

    Guide to Programming with Python ))

    he range() &unction

    >>> range(5)

    [0, 1, 2, 3, 4]

    >>> range(0, 50, 5)

    [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

    • 3eturns a sequence of integers in range

    • range(i) returns sequence 0 through i – 1• range(i, j) returns sequence i through j – 1

    • range(i, j, k) returns sequence i to j - 11 step k 

  • 8/20/2019 Word Jumble Game in Python

    12/48

    Guide to Programming with Python )2

    Counting &orward1 Ay &ives1 and

    Aac,wards

    # o!nting forward

    for i in range(10):

    print i,

    # o!nting " fi$e%

    for i in range(0, 50, 5):

    print i,

    # o!nting "a&ward%

    for i in range(10, 0, '1):

    print i,

  • 8/20/2019 Word Jumble Game in Python

    13/48

    Guide to Programming with Python )#

    Using +equence Operators and

    &unctions with +trings

    • Python has functions and operators that wor, with

    sequences

    • Can tell you things such as

     0  /ength of sequence 0  !f contains specific element

  • 8/20/2019 Word Jumble Game in Python

    14/48

    Guide to Programming with Python )'

    he Bessage 6nalyer Program

    &igure '('* +ample run of the Bessage 6nalyer program

    len() function and in operator produce information about a message(

  • 8/20/2019 Word Jumble Game in Python

    15/48

    Guide to Programming with Python )5

    Using the len() function

    >>> len(a*e +$er)

    10

    • a,es a sequence• 3eturns the number of elements

    • !n strings1 every character counts 0 spaces and

    punctuation

  • 8/20/2019 Word Jumble Game in Python

    16/48

    Guide to Programming with Python )7

    Using the in Operator 

    >>> e in a*e +$er

    -r!e

    • ests for element membership 0 3eturns -r!e if element is in sequence

     0 3eturns .al%e otherwise

  • 8/20/2019 Word Jumble Game in Python

    17/48

    Guide to Programming with Python )9

    !nde"ing +trings

    • Sequential access: 6ccess in order 

    • Random access: ;irect access to any element

    • Indexing: Process used to access a specific

    element of a sequence

    • Member: 6n element of a sequence

    • Python allows for random access to sequences

  • 8/20/2019 Word Jumble Game in Python

    18/48

    Guide to Programming with Python )>

    he 3andom 6ccess Program

    &igure '(5* +ample run of the 3andom 6ccess program

    Dou can directly access any character in a string through inde"ing(

  • 8/20/2019 Word Jumble Game in Python

    19/48

    Guide to Programming with Python )?

    $or,ing with Positive Position

    Eumbers

    >>> word / inde

    >>> word[3]

    e

    • Use brac,ets and position number to inde"

    • !nde"ing for positive position numbers starts at 0

    • /ength of sequence minus one is last position

    •  6ttempt to access beyond last position results inerror 

  • 8/20/2019 Word Jumble Game in Python

    20/48

    Guide to Programming with Python 2@

    $or,ing with Eegative Position

    Eumbers

    >>> word / inde

    >>> word['2]

    e

    • Can use negative position numbers

    • +tart at end of sequence with position number* –1

    • 4nd at first element1 with position number* negative

    sequence length

  • 8/20/2019 Word Jumble Game in Python

    21/48

    Guide to Programming with Python 2)

    Positive and Eegative Position

    Eumbers

    &igure '(7* +equence !nde"ing

  • 8/20/2019 Word Jumble Game in Python

    22/48

    Guide to Programming with Python 22

    +tring !mmutability

    >>> word / ga*e

    >>> word[0] / l

    -perror: o"et doe% not %!pport ite* a%%ign*ent

    • Mutable: Changeable

    • Immutable: Unchangeable

    • +trings are immutable sequences: cant be

    changed• Aut can create new strings from e"isting ones

  • 8/20/2019 Word Jumble Game in Python

    23/48

    Guide to Programming with Python 2#

    +tring !mmutability

  • 8/20/2019 Word Jumble Game in Python

    24/48

    Guide to Programming with Python 2'

    Auilding a Eew +tring

    • CanFt modify an e"isting string

    • Aut can build

  • 8/20/2019 Word Jumble Game in Python

    25/48

    Guide to Programming with Python 25

    he Eo Howels Program

    &igure '(>* +ample run of Eo Howels program

    Eew strings are created through concatenation(

  • 8/20/2019 Word Jumble Game in Python

    26/48

    Guide to Programming with Python 27

    Constants

    +67 / aeio!

    • onstant: Eame associated with value not meant

    to be changed• Convention is to use all uppercase variable names

    • Can ma,e programs clearer

    • +aves retyping

  • 8/20/2019 Word Jumble Game in Python

    27/48

    Guide to Programming with Python 29

    Creating Eew +trings from 4"isting

    Ones

    new8*e%%age 9/ letter

    • Concatenation creates brand8new string

    • 3emember1 strings are immutable• +o1 new8*e%%age becomes the newly created string

    resulting from concatenation

  • 8/20/2019 Word Jumble Game in Python

    28/48

    Guide to Programming with Python 2>

    +licing +trings

    • Slice: Copy of continuous section of a sequence

    • Can ma,e slices

  • 8/20/2019 Word Jumble Game in Python

    29/48

    Guide to Programming with Python 2?

    he Pia +licer Program

    &igure '(?* +ample run of the Pia +licer program

    &resh1 hot slices of pia1 made just the way you as,ed(

  • 8/20/2019 Word Jumble Game in Python

    30/48

    Guide to Programming with Python #@

    one

    • 3epresenting nothing

    • Ba,es a good placeholder for a value

    • 4valuates to .al%e when treated as a condition

  • 8/20/2019 Word Jumble Game in Python

    31/48

    Guide to Programming with Python #)

    +licing

    &igure '()@* +licing end points

     6n e"ample of slicing end point numbers for the string pi;;a(

    QuickTime™ and a decompressor

    are needed to see this picture.

    QuickTime™ and a decompressor

    are needed to see this picture.

  • 8/20/2019 Word Jumble Game in Python

    32/48

    Guide to Programming with Python #2

    +licing >> word / pi;;a

    >>> print word[0:5]

    pi;;a

    >>> print word[1:3]

    i;

    >>> print word['4:3]

    i;

    • Can give start and end position• +lice is a brand8new sequence

  • 8/20/2019 Word Jumble Game in Python

    33/48

    Guide to Programming with Python ##

    +licing >> word / pi;;a

    >>> word[:4]

    pi;;

    >>> word[2:]

    ;;a

    >>> word[:]

    pi;;a

    • Can omit the beginning point• Can omit the ending point

    • sequence[:] is copy of sequence 

  • 8/20/2019 Word Jumble Game in Python

    34/48

    Guide to Programming with Python #'

    Creating uples

    • !uple: !mmutable sequence of values of any type

    • Could have tuple of integers for a high score list1

    for e"ample

    • uples elements donFt need to all be of same type

  • 8/20/2019 Word Jumble Game in Python

    35/48

    Guide to Programming with Python #5

    he Ieros !nventory Program

    &igure '())* +ample run of the Ieros !nventory Program

    he heros inventory is represented by a tuple of strings(

  • 8/20/2019 Word Jumble Game in Python

    36/48

    Guide to Programming with Python #7

    uple Aasics

    • Creating an 4mpty uplein$entor / ()

    • reating a uple as a Condition

    if not in$entor:print

  • 8/20/2019 Word Jumble Game in Python

    37/48

    Guide to Programming with Python #9

    uple Aasics

  • 8/20/2019 Word Jumble Game in Python

    38/48

    Guide to Programming with Python #>

    Using uples

    • uples are a ,ind of sequence

  • 8/20/2019 Word Jumble Game in Python

    39/48

    Guide to Programming with Python #?

    he Ieros !nventory 2(@

    &igure '()2* +ample run of the Ieros !nventory program

    ;emonstrates inde"ing1 slicing1 and concatenating tuples

  • 8/20/2019 Word Jumble Game in Python

    40/48

    Guide to Programming with Python '@

    Using len() and in with uples

    • he len() function with tuples

     0 %ust as with strings1 returns number of elements

    print

  • 8/20/2019 Word Jumble Game in Python

    41/48

    Guide to Programming with Python ')

    !nde"ing uples

    &igure '()#* 4ach element has a corresponding position number(

    4ach string is a single element in the tuple(

  • 8/20/2019 Word Jumble Game in Python

    42/48

    Guide to Programming with Python '2

    +licing uples

    &igure '()'* +licing positions defined between elements

    uple slicing wor,s much li,e string slicing(

    QuickTime™ and a decompressor

    are needed to see this picture.

    QuickTime™ and a decompressor

    are needed to see this picture.

    QuickTime™ and a de compressor

    are needed to se e this picture.

    QuickTime™ and a decompressor

    are needed to see this picture.

    QuickTime™and a decompressor

    areneeded to see this picture.

  • 8/20/2019 Word Jumble Game in Python

    43/48

    Guide to Programming with Python '#

    uple !mmutability

    >>> in$entor / (%word, ar*or, %hield,

    healing potion)

    >>> in$entor[0] / "attlea

    -perror: o"et doe%nt %!pport ite* a%%ign*ent

    • uples are immutable

    • Aut can create new tuples from e"isting ones

  • 8/20/2019 Word Jumble Game in Python

    44/48

    Guide to Programming with Python ''

    Concatenating uples

    >>> in$entor / (%word, ar*or, %hield,

    healing potion)

    >>> he%t / (gold, ge*%)

    >>> in$entor 9/ he%t

    >>> print in$entor

    (%word, ar*or, %hield, healing potion,

    gold, ge*%)

    • Concatenation operator1 91 wor,s with tuples justli,e with strings

  • 8/20/2019 Word Jumble Game in Python

    45/48

    Guide to Programming with Python '5

    3eview wordJjumble(py

  • 8/20/2019 Word Jumble Game in Python

    46/48

    Guide to Programming with Python '7

    +ummary

    •  6n ordered list of elements is called whatK

     0 6 sequence

    • o move through a sequence1 in order1 is called whatK

     0 !terate• $hen a for loop iterates over a sequence1 how many

    times does it perform its loop bodyK

     0 6s many times as there are elements in the sequence

    • $hat would range(20,10,'2) returnK– [20, 1?, 1@, 14, 12]

    • $hat would len(range(20,10,'2)) returnK– 5

  • 8/20/2019 Word Jumble Game in Python

    47/48

    Guide to Programming with Python '9

    +ummary

  • 8/20/2019 Word Jumble Game in Python

    48/48

    +ummary