Using other loops and conditionals
Python offers a variety of ways to iterate through information in addition to the for
loop. One of the commonly used loops is a while
loop. When using a while
loop, we're checking for a condition constantly. Again, it's easier to understand this by looking at an example.
Let's say we were playing a game and ask the user to provide as many animals as they can. We'd want to be able to do a few things:
- Check whether the player has animals to add.
- If yes, append each animal to a list.
- If no, end the program and provide the final list.
- Provide the final list and a count for how many animals the player was able to enter.
Let's take a look at an algorithm that does the basics for this game:
ch10_whileLoop1.py
myAnimals = [] print('Let\'s see how many animals you can name. Get ready!') readyPlayer = input('When you are ready to begin, type y. ') while readyPlayer == &apos...