Description: This in-class question demonstrates While Loops in Python.
Instructor: Dr. Ana Bell
The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseWare at ocw.mit.edu.
ANA BELL: So let's look at the example, or the in-class exercise, with while loops. Oh good, we got some varying responses here, perfect. So the code is, "You're in the Lost Forest. Go left or right?" OK. So here, the while loop is checking if the input is equal to this particular string. OK. And if it is, type the same thing again and ask them for more input.
So my question was, what happens when you type in "R-i-g-h-t?" And I think the majority of the class is getting it right. And maybe the people who answered this are maybe changing their answer to something else, but you're right.
So you ask the question again, "Go left or right?" And that's because Python, again, is very, very particular. We're telling it that the user input must match this string exactly. So "R-i-g-h-t" is going to-- even though it says right, it doesn't match up.
So if you wanted-- I'll just show you in this example here-- if you wanted to take care of that case, then you'd add this little-- you'd expand on this line right here, which says, "while n = = 'right' or n = = to "R-i-g-h-t" or whatever other cases you'd want to take care of. OK.