CS 111 f21 — while
loops
1 Poll
- What will be printed?1
s = "that's good soup!" acc = "mmm" for i in range(1, len(s), 2): acc = acc + s[i] print(acc)
- What will be printed?2
def zero(vec): for i in range(len(vec)): vec[i] = 0 vec = [1,2,3] print(zero(vec))
- What will be printed?3
def swap_first(xs, ys): t = xs[0] xs[0] = ys[0] ys[0] = t v1 = [1, 2, 3] v2 = ("a", "b", "c") swap_first(v1, v2) print(v1)
2 Return to the guessing game
Generate a random secret number, have the player try and guess it
2.1 Validate input
guess = input("Guess a number ") while not guess.isdecimal() or int(guess) < 0 or int(guess) > 100: guess = input("Guess a number ") guess = int(guess)
2.2 while
loop
while CONDITION:
BODY
- Each time through the loop (including the first time),
CONDITION
is evaluated - if
CONDITION
isTrue
, the loop goes throughBODY
again - if
CONDITION
isFalse
, the program skips to the first unindented line after the loop while True:
— infinite loop???
2.3 Game loop
import random def get_guess(): guess = input("Guess a number ") while not guess.isdecimal() or int(guess) < 0 or int(guess) > 100: guess = input("Guess a number ") guess = int(guess) return guess secret_num = random.randint(0, 100) tries = 10 guess = get_guess() while tries > 0 and guess != secret_num: tries -= 1 diff = secret_num - guess diff = abs(diff) if guess != secret_num: if diff <= 5: print("lava") elif diff <= 10: print("warm") else: print("cold") guess = get_guess() if guess == secret_num: print("YOU GOT IT!!!") else: print("Better luck next time. The number was", secret_num)
3 Practice
- poll: infinite while loop (no increment)
- translate this for loop into a while loop:
total = 0 for i in range(10): total += i print(total)
total = 0 i = 0 while i < 10: total += i i += 1 print(total)
4 Review
4.1 is_valid_variable_name
I'm trying to write a function that takes a string and returns whether that string would be a valid variable name. Remember that a variable name can only have numbers, letters, and underscores. In addition, a variable name cannot start with a number. Describe why each of the versions below will not return the correct result.4
def is_valid_variable_name1(name): if name[0].isdecimal(): return False for c in name: if c.isalnum() or c == "_": return True else: return False def is_valid_variable_name2(name): ok_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" return name[0] in ok_characters and name in ok_characters def is_valid_variable_name3(name): result = True for c in name: result = c.isalnum() result = c == "_" return result and name[0].isdecimal()
4.2 List Mystery
What will be printed by the following code?5
v = [4, 5, 6, 7, 8, 9] v[0] = v[-1] - v[-2] print(v) i = v[0] + 2 v[i] = v[i - 2] - v[i - 3] print(v) v[len(v) - 2] = i * 2 print(v)
Footnotes:
1
mmmhtsgo op
3
There will be a TypeError
because tuples are immutable and thus ys[0] = t
causes an error when ys
is a tuple. If we changed v2
to be a list, the program would work and ["a", 2, 3]
would be printed.
4
is_valid_variable_name1
: this will return after checking only the first character in the loop.# fixed version: def is_valid_variable_name1(name): if not (name[0].isalpha() or name[0] == "_"): return False for c in name: if not (c.isalnum() or c == "_"): return False return True
is_valid_variable_name2
:name in ok_characters
checks if the entire name appears inok_characters
, rather than checking whether each character ofname
is inok_characters
. Also fails to check that the first characters is not a number.is_valid_variable_name3
:# fixed version: def is_valid_variable_name3(name): result = True for c in name: result = result and (c.isalnum() or c == "_") return result and not name[0].isdecimal()
5
[1, 5, 6, 7, 8, 9] [1, 5, 6, 4, 8, 9] [1, 5, 6, 4, 6, 9]