打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Exercise 33: While Loops

Now to totally blow your mind with a new loop, the while-loop. A while-loop will keep executing the code block under it as long as a boolean expression is True.

Wait, you have been keeping up with the terminology, right? That if we write a line and end it with a : (colon) then that tells Python to start a new block of code? Then we indent and that's the new code. This is all about structuring your programs so that Python knows what you mean. If you do not get that idea then go back and do some more work with if-statements, functions, and the for-loop until you get it.

Later on we'll have some exercises that will train your brain to read these structures, similar to how we burned boolean expressions into your brain.

Back to while-loops. What they do is simply do a test like an if-statement, but instead of running the code block once, they jump back to the "top" where the while is, and repeat. It keeps doing this until the expression is False.

Here's the problem with while-loops: Sometimes they do not stop. This is great if your intention is to just keep looping until the end of the universe. Otherwise you almost always want your loops to end eventually.

To avoid these problems, there's some rules to follow:

  1. Make sure that you use while-loops sparingly. Usually a for-loop is better.
  2. Review your while statements and make sure that the thing you are testing will become False at some point.
  3. When in doubt, print out your test variable at the top and bottom of the while-loop to see what it's doing.

In this exercise, you will learn the while-loop by doing the above three things:

 1 2 3 4 5 6 7 8 910111213141516
i = 0numbers = []while i < 6:    print "At the top i is %d" % i    numbers.append(i)    i = i   1    print "Numbers now: ", numbers    print "At the bottom i is %d" % iprint "The numbers: "for num in numbers:    print num

What You Should See

$ python ex33.pyAt the top i is 0Numbers now:  [0]At the bottom i is 1At the top i is 1Numbers now:  [0, 1]At the bottom i is 2At the top i is 2Numbers now:  [0, 1, 2]At the bottom i is 3At the top i is 3Numbers now:  [0, 1, 2, 3]At the bottom i is 4At the top i is 4Numbers now:  [0, 1, 2, 3, 4]At the bottom i is 5At the top i is 5Numbers now:  [0, 1, 2, 3, 4, 5]At the bottom i is 6The numbers:012345

Study Drills

  1. Convert this while-loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.
  2. Now use this function to rewrite the script to try different numbers.
  3. Add another variable to the function arguments that you can pass in that lets you change the 1 on line 8 so you can change how much it increments by.
  4. Rewrite the script again to use this function to see what effect that has.
  5. Now, write it to use for-loops and range instead. Do you need the incrementor in the middle anymore? What happens if you do not get rid of it?

If at any time that you are doing this it goes crazy (it probably will), just hold down CTRL and hit c (CTRL-c) and the program will abort.

Common Student Questions

What's the difference between a for-loop and a while-loop?
A for-loop can only iterate (loop) "over" collections of things. A while-loop can do any kind of iteration (looping) you want. However, while-loops are harder to get right and you normally can get many things done with for-loops.
Loops are hard. How do I figure them out?
The main reason people don't understand loops is because they can't follow the "jumping" that the code does. When a loop runs, it goes through its block of code, and at the end it jumps back to the top. To visualize this, put print statements all over the loop printing out where in the loop Python is running and what the variables are set to at those points. Put prints before the loop, at the top of the loop, in the middle, and at the bottom. Study the output and try to understand the jumping that's going on.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【引用】Loop
【loop
Fahrenheit_to_Celsius__loop
Python Tips, Tricks, and Hacks
博客园 - Dflying Chen - 非常有用的101道算法部分常见面试题
W3CHINA.ORG讨论区--微软101道经典面试题-----3
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服