Lecture 20: While Loops
(Sections 7.3, 7.4)
CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 20: While Loops (Sections 7.3, 7.4) CS 1110 Introduction - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 20: While Loops (Sections 7.3, 7.4) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements A3 is
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
grades has more elements put next element in x
True False
print(x)
3
4
5
condition
True False
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random, hangman word_list = [ … words we want user to guess .. ] N_GUESSES = 10 secret = hangman.SecretWord(random.choice(word_list)) for n in list(range(N_GUESSES)): secret.word_so_far() user_guess = input("Guess a letter: ") secret.apply_guess(user_guess): if secret.is_solved(): print("YOU WIN!!!") break #jumps out of the for-loop secret.reveal()
20
import random, hangman word_list = [ … words we want user to guess .. ] N_GUESSES = 10 secret = hangman.SecretWord(random.choice(word_list)) for n in list(range(N_GUESSES)): secret.word_so_far() user_guess = input("Guess a letter: ") secret.apply_guess(user_guess) if secret.is_solved(): print("YOU WIN!!!") break #jumps out of the loop secret.reveal()
21
while n_strikes < MAX_STRIKES:
n_strikes = 0
bad_guess =
if bad_guess: n_strikes = n_strikes + 1
MAX_STRIKES = 10
§ More natural than range § Works better with deletion
§ Loop until calculation done § Exact steps are unknown
§ Just set loop var to False
§ Easy to forget loop vars § Or get stop condition wrong
§ Initialize the condition? § Update the condition?
22