Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Dancing with Python

You're reading from   Dancing with Python Learn to code with Python and Quantum Computing

Arrow left icon
Product type Paperback
Published in Aug 2021
Publisher Packt
ISBN-13 9781801077859
Length 744 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Robert S. Sutor Robert S. Sutor
Author Profile Icon Robert S. Sutor
Robert S. Sutor
Arrow right icon
View More author details
Toc

Table of Contents (29) Chapters Close

Preface 1. Chapter 1: Doing the Things That Coders Do 2. Part I: Getting to Know Python FREE CHAPTER
3. Chapter 2: Working with Expressions 4. Chapter 3: Collecting Things Together 5. Chapter 4: Stringing You Along 6. Chapter 5: Computing and Calculating 7. Chapter 6: Defining and Using Functions 8. Chapter 7: Organizing Objects into Classes 9. Chapter 8: Working with Files 10. PART II: Algorithms and Circuits
11. Chapter 9: Understanding Gates and Circuits 12. Chapter 10: Optimizing and Testing Your Code 13. Chapter 11: Searching for the Quantum Improvement 14. PART III: Advanced Features and Libraries
15. Chapter 12: Searching and Changing Text 16. Chapter 13: Creating Plots and Charts 17. Chapter 14: Analyzing Data 18. Chapter 15: Learning, Briefly 19. References
20. Other Books You May Enjoy
21. Index
Appendices
1. Appendix A: Tools 2. Appendix B: Staying Current 3. Appendix C: The Complete UniPoly Class
4. Appendix D: The Complete Guitar Class Hierarchy
5. Appendix E: Notices 6. Appendix F: Production Notes

1.6 Conditional processing

When you write code, you create instructions for a computer that state what steps the computer must take to accomplish some task. In that sense, you are specifying a recipe. Depending on some conditions, like if a bank balance is positive or a car’s speed exceeds a stated limit, the sequence of steps may vary. We call how your code moves from one step to another its flow.

Let’s make some bread to show where a basic recipe has points where we should make choices.

Photo of two loaves of bread
Figure 1.1: Loaves of bread. Photo by the author.

Ingredients

  • 1 ½ tablespoons sugar
  • ¾ cup warm water (slightly warmer than body temperature)
  • 2 ¼ teaspoons active dry yeast (1 package)
  • 1 teaspoon salt
  • ½ cup milk
  • 1 tablespoon unsalted butter
  • 2 cups all-purpose flour

Straight-through recipe

  1. Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes.
  2. Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes.
  3. Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes.
  4. Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
  5. Cover the bowl and let it sit in a warm place for 1 hour.
  6. Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes.
  7. Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes.
  8. Turn out the loaf of bread and let it cool.

Problems

I call that a “straight-through” recipe because you do one step after another from the beginning to the end. The flow proceeds in a straight line with no detours. However, the recipe is too simple because it does not include conditions that must be met before moving to later steps.

  • In step 1, your bread will not rise if the yeast is dead. We must ensure that the mixture is active and foamy.
  • In step 2, we need to add more flour in little portions until the stirred dough pulls away from the bowl.
  • In step 3, if the dough gets too sticky, we need to add even more flour.
  • In step 5, an hour is only an estimate for the time it will take for the dough to double in size. It may take more or less time.
  • In step 6, we want the dough to double in size in the loaf pan.
  • In step 7, 45 minutes is an estimate for the baking time for the top of the bread to turn golden brown.

Conditional recipe

  1. Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes. If the mixture is foamy, then continue to the next step. Otherwise, discard the mixture, and combine sugar, water, and fresh yeast in a large bowl. Let sit for 10 minutes.
  2. Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes. If the mixture does not pull aside from the sides of the bowl, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
  3. Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes. If the mixture is sticky, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
  4. Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
  5. Cover the bowl and let it sit in a warm place for 1 hour. If the dough has not doubled in size, then let it sit covered in the bowl for 15 more minutes. Otherwise, continue to the next step.
  6. Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes. If the dough has not doubled in size, then let it sit covered in the baking pan for 15 more minutes. Otherwise, continue to the next step.
  7. Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes. If the top of the bread is not golden brown, then bake for 5 more minutes. Otherwise, continue to the next step.
  8. Turn out the loaf of bread and let cool.

Figure 1.2 is a flowchart, and it shows what is happening in the first step. The rectangles are things to do, and the diamond is a decision to be made that includes a condition.

A basic flowchart with a condition
Figure 1.2: A flowchart for the first step of bread making

You may find it useful to use such a chart to map out the flow of your code.

These instructions are better. We test several conditions to determine the next course of action. We still are not checking whether a condition is met after repeating a step multiple times.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime