Coding for Kids: Python: Learn to Code with 50 Awesome Games and Activities by Adrienne B. Tacke
Author:Adrienne B. Tacke [Tacke, Adrienne B.]
Language: eng
Format: epub
Publisher: Rockridge Press
Published: 2020-06-20T22:00:00+00:00
As we iterate through our loop, we ask the computer the same question: âHey, if you add 2 to the next number in the numbers list, will the result be less than 20?â If it is, we finally get to the print() function and print that number. Again, we indent this code, as it is a new block that only runs after passing the first two levels of Boolean expressions.
numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
i = 0
while(i < len(numbers)):
if (numbers[i] + 2) < 20:
print(numbers[i] + 2)
Finally, and probably the most important part of while loops, we have to add code to increment our iterator variable! Remember, we are the ones keeping track of how many loops we have repeated. We also know how important the iterator variable is, because we use it in our while loopâs Boolean expression and as our index in our if statementâs Boolean expression:
numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
i = 0
while(i < len(numbers)):
if (numbers[i] + 2) < 20:
print(numbers[i] + 2)
i += 1
Notice how I put this code on the same indentation level as the if statement? Because there is no other code that needs to be repeated, and because we always want to keep count of how many times we have repeated our loop, we place the increment code (i += 1 ) here right before it starts the loop all over again.
Now, I said this was the most important part of while loops. Why? Try running the while loop without the code to increment our iterator variable. What happens ?
Youâve just encountered your first infinite loop ! Your shell will just keep printing the same number over and over again forever:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6781)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6508)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6376)
Kotlin in Action by Dmitry Jemerov(5061)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3975)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3761)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3702)
The Ultimate iOS Interview Playbook by Avi Tsadok(3678)
