{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

slides7-4up[1]

# slides7-4up[1] - Compsci 6/101 I Loop over sequence with...

This preview shows pages 1–2. Sign up to view the full content.

Compsci 06/101, Spring 2011 7.1 Compsci 6/101: I Python Techniques for looping Loop over sequences by sequence value Loop by indexing, or by index and value: enumerate While loop: as long as condition holds, e.g., game not over Techniques for transforming data One domain leads to solutions, other much harder Identify music with sound-hound/shazaam Encryption: transform data to hide it, but … APT AnagramFree Compsci 06/101, Spring 2011 7.2 Loop over sequence with index Index useful in accessing elements in order Sometimes need adjacent elements, i-1 , i , and i+1 Often need both index and element, see enumerate below for i,fr in enumerate(['a','b','c']): print i,fr No more powerful than looping over range, why? Idiomatic programming, helps to know vocabulary Syntactic sugar Not necessary, use for i in range(0,len(seq)): Compsci 06/101, Spring 2011 7.3 Indefinite loop: while interactivity wrong = 0 while wrong < max_wrong: guess = raw_input() if not good_guess(guess): wrong += 1 else: #process the guess here Suppose, for example, play http://www.hangman.no What happens if you loop while True: Break out of loop with break See code in GuessNumber.py

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

slides7-4up[1] - Compsci 6/101 I Loop over sequence with...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online