--Originally published at Hector Martinez Alcantara
This post is about the other loop, the while loop, it’s often used to do an action until a variable changes it’s value.
Here’s the sintaxis of the while loop
while condition:
statements
And here’s an example of this:
#Introducing 5 lines of text
list=[]
x=1
while x<6:
list.append(input("type some words"))
x=x+1
print(list)
An example of while used as a returning menu:
op=""
while op!= "c":
print("a)Print Hello")
print("b)Print something")
print("c)Exit")
op=input("Select an option\n")
op=op.lower()
if(op=="a"):
print("Hello")
elif(op=="b"):
print("Something")
elif(op=="c"):
print("Goodbye")
else:
print("Thats not a letter of the menu")
The result is
a)Print Hello b)Print something c)Exit Select an option >>a Hello a)Print Hello b)Print something c)Exit Select an option >>b Something a)Print Hello b)Print something c)Exit Select an option >>z Thats not a letter of the menu a)Print Hello b)Print something c)Exit Select an option >>c Goodbye
Thanks for reading, ask me in comments.
Special thanks to Python while loop from Tutorialspoint
