Module 4 - Section 1 - While and Functions
4.1.2 - Break
Another way to get out of a loop is to use the command break. So if we want a specific reply from a user then we can ask and if we get a wrong answer then we inform and ask again.
Copy the following code to Thonny and save it as test_while2.py (or whatever name you like) :
As an exercise create a code that asks the user to input his name and then his favorite color and then display a message "Hi name! Your favorite color is color! After that ask the player if he wants to play again and if he replies yes then ask again name and color if replies no exit and in any other case ask again.
- create a variable game and assign it True (when it turns to False game stops)
- create a while loop to check game variable
- input name and color to two variables
- create a variable cont to ask user if he want to continue
- with a while loop ask user if he wants to continue
You can view a sample solution here.