Does the “while” loop need to be run with boolean statements?
Yes - the while
loop requires a boolean condition.
Not a question but an idea for how to make an infinite loop in a for loop: i think you can just have a expression in the body of the loop that resets i to a lower number and it is stuck doing the loop.
Great observation! Now promise never to do this!
Is there ever a time when an infinite loop would be at all useful?
No, unless you want to create an immortal program (insert evil scientist laughter).
What exactly does ctrl + C do?
Sends an abort signal to the operating system
Could we combine boolean flags with while loops?
Yes! This is a typical usage. We may use the boolean flag to end the program. For example, we could extend our contains_a
program to stop as soon it finds a vowel. We would need a while loop to do this.
Can you put limits on both ends in while loops?
I think you are asking if we can put bounds on the loop variable. We could do this using and
(alternatively, or
). For example, we could have a loop condition that stops if a variable is outside of a range:
while x < 5 and x > 0:
#do some work
How do we generate a random number in python? What code would we write if we wanted to simulate rolling a die?
We’ll discuss this Wednesday!
How can boolean expression be assigned to a variable while they still have =
in it? For example, var = age = 18
We use ==
to check if two values are equal to each other. =
means assignment. So your expression would set age
to 18
, then set var
to be the same as age
(so also 18
).
Are (when are) boolean flags necessary? Certainly contains_a.py could be written without one.
The only other way to solve the program would be to use an accumulator. In that case, you are essentially treating your accumulator as a proxy for a boolean flag. This is pretty common - but is often more error prone and less predictable. You can always ask “how many a’s” and convert it to boolean later.
would we use the break command in a for or while loop?
Python does have the break
command. We’ll learn about it later - students often overly rely on it leading to poorly designed solutions. It can useful, though.
Where are while loops really helpful in real world problems?
When playing hoops, I like to yell “A 3 for the victory!” Since I am bad, I ignore misses and just repeat until I actually make it and then high-five myself.
We’ll use while
or for
loops for almost every algorithm going forward, including searching for a value in a large list as well as sorting (or ordering) a list of items.
How would infinity be represented or expressed?
It can be exactly represented since we don’t have infinite bits of memory. But most languages replace infinity with some special value that stands in for infinity. You can use float('inf')
or float('-inf')
to obtain such a value.
Are there more kinds of loops for different situations?
There are only definite or indefinite loops, so we won’t need any other options.
Why do we have to change the loop variable inside a while loop?
If you don’t change the value, then the boolean condition we check at the top of the while
loop will never change in value. This gives us an infinite loop.
once people learn while loops, do they still use for loops?
Much simpler to read and it is almost impossible to really screw up a for loop. While loops are common sources for errors in programs.
why setting i = 5 and then proceeding with the while loop didn’t do anything
Oh, it was because the while loop condition was while i > 5
which is False
before we ever get in to the loop.
When would I use complicated boolean logic statements like the last problem on today’s quiz?
You might need to for the lab! Or, if you need to check multiple things at once for example, if you require passwords to have at least 8 characters and at least one upper-case letter or a number and no question marks are allowed.
How do you know when to use a for loop and when to use a while loop?
If you know how many times the loop will run (e.g., for each character in a string, or 20 times) then use a for loop. Otherwise, you can’t define the range so you’ll have to use a while loop.
How exactly did you create the infinite list with the while loop
The loop asks each time whether i < 5
. Since I set i = 0
, this value is True
and so the loop prints out the value 0. Since I never update the value of i
, the next iteration of the loop will ask the same question, come up with the same answer, and print the same value. This keeps repeating.
When would it be better to simply use a break statement to end a loop versus setting the while loop variable outside its parameters?
This is a very messy way to handle loops. There are good reasons to use breaks, but it is rarely a good idea to use them in place of a carefully designed while statement condition.
how could you take the absolute value of something (like the tax)?
The abs(x)
function will give you the absolute value of some number x
what’s up with if hasA: not having to be set with anything?
hasA
is a boolean variable, and a boolean value is all that is required for an if statement. It’s the same as the two options below - we can use a variable to save the result of an expression and then just use the variable.
print(5+3)
x = 5+3
print(x)
In what ways do people utilize infinite loops in their programs?
To cause their professors to drink. They are to be avoided, not encouraged :)