Python: While Loops

  1. Introduction to Python
  2. Python: Choosing a Text Editor or IDE
  3. Python: Hello World
  4. Python: Variables, Strings and Numbers
  5. Python: Variable Naming
  6. Python: Math, Familiar
  7. Python: Math, Less Familiar
  8. Python: Mathematical Order of Operations
  9. Python: Introducing PEP 8
  10. Python: Text Concatenation
  11. Python: if Statements and Comparison Operators
  12. Python: else and elif statements
  13. Python: Testing Multiple Conditions
  14. Python: Testing Sets of Conditions
  15. Python: Nested if Statements
  16. Python: Lists
  17. Python: Adding To and Changing Lists
  18. Python: Lists: Take a Slice, Delete Elements, Popping Elements
  19. Python: Tuples
  20. Python: for Loops
  21. Python: Nested for Loops
  22. Python: Capturing and Formatting User Input
  23. Python: Dictionaries
  24. Python: Functions
  25. Python: While Loops
  26. Python: Creating and Using Classes
  27. Python: Data Files
  28. Python: Modules
  29. Python: CSV Files
  30. Python: JSON Files
  31. Python: Errors and Exception Handling
  32. Python: Using Pexpect
  33. Python : Using Pexpect : ftpTestOffload.sh
  34. Python : Using Pexpect: ftpTest.py
  35. Python: DCL Conversion to Python

Go to Chapter 51 of A Smarter Way.

Recall for loops:
http://localhost/2018/10/02/python-for-loops/

for friend in friends:
    print("Hello, " + friend.title() + "!")

For loops are good for meeting a single condition, once. But sometimes you need to keep performing the matching operation. This is where while loops are more useful.

# A list to test against
my_cats = ["Betty", "Ginger", "Nico", "Sven", "Hudson"]

# If you're going to be accepting repeat input, 
#you'll need to initialize a variable# as an empty value.user_input = ""while user_input != "q":    user_input = input("Enter a cat name, or q to quit: ")
    if user_input != "q":
        for one_cat in my_cats:
            if user_input == one_cat:
                print(one_cat + " is one of my cats")
                break

Indent carefully!

Go to Chapter 51 of A Smarter Way.

Setting a Flag to Continue or End Looping

# Set a flag to control looping
keep_looping = True
while keep_looping == True: 
    user_input = input("Enter a cat name, or q to quit: ")
    if user_input != "q":
        for one_cat in my_cats:
            if user_input == one_cat:
                print(one_cat + " is one of my cats")
        break
else:
    keep_looping = False

This is a useful place to use a file to supply input. See this example from
http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line-by-line-in-python/:

# Open the file read-only
f = open('input_file.txt')

# use readline() to read the first line
line = f.readline()

# If the file is not empty keep reading one line
# at a time, till the file is empty
while line:
    # In python 2+ use the syntax:
    # print line
    # In python 3 print is a builtin function, 
    # So use this syntax:
    print(line)
    # Then use realine() to read next line
    line = f.readline()
f.close()

The article linked above provides more examples that handle the input file differently. Read it.