๐Ÿ“– Beginning Python Programming /
Module: Controlling logic flow

while ...:

๐Ÿ“– Beginning Python Programming / Controlling logic flow / while ...:

Code example: While-Input-List v3

items = []

list_name = input("Please enter a list name: ")

while True:
    value = input(f"Please input an item for {list_name}, or 'q' to quit: ")
    if value == "q":
        break
    if len(value) > 0:
        items.append(value)

with open(f"{list_name}.txt", "a") as file_obj:
    file_obj.write("\n".join(items))
    file_obj.write("\n")

print(f"List saved to {list_name}.txt.")
In this code example, we use something new:
  • File operations
  • with...as... syntax

We will learn more about it in module 4.