xxxxxxxxxx
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
break
xxxxxxxxxx
while <condition>:
<run code here>
# for example,
i = 0
while True:
i += 1
# i will begin to count up to infinity
while i == -1:
print("impossible!")
xxxxxxxxxx
# A while loop is basically a "if" statement that will repeat itself
# It will continue iterating over itself untill the condition is False
python_is_cool = True
first_time = True
while python_is_cool:
if first_time:
print("python is cool!")
else:
first_time = False
print("Done")
# The while loop can be terminated with a "break" statement.
# In such cases, the "else" part is ignored.
# Hence, a while loop's "else" part runs if no break occurs and the condition is False.
# Example to illustrate the use of else statement with the while loop:
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
xxxxxxxxxx
#there is no do while loop in python but you san simulate it
i = 1
while True:
print(i)
i = i + 1
if(i > 5):
break
xxxxxxxxxx
##################################
# THERE IS NO DO-WHILE IN PYTHON #
##################################
## BEACUSE YOU DON'T NEED IT
# Here is the alternative
while True: # INFINITE LOOP (not really) # This is like do
'''
stuff to do
'''
if not (condition): # this is like while (condition)
break
xxxxxxxxxx
while <Condition>:
<code>
#example
i = 10
while i == 10:
print("Hello World!")
xxxxxxxxxx
i = 1
while i<10:
print("hello world")
i+=1
#note the i++ syntax is not valid in python
xxxxxxxxxx
# while True loop
while True:
print("This will continue to print this forever")
# while variable loop
value = 5
while value = 5:
# Since this is true, this will continue on forever
print("Yes")
xxxxxxxxxx
print("the secret word rhymes with opinion and likes bananas")
secret_word = "minion"
guess = ""
while guess != secret_word:
guess = input("Enter your guess:")
if guess == secret_word:
print("you win!!!")
break
else:
print("wrong guess again!!!")