Skip to content

Instantly share code, notes, and snippets.

@lfalanga
Last active December 15, 2019 02:15
Show Gist options
  • Save lfalanga/0ccb2bf7d4c5a9f44cc660dc0517c0a1 to your computer and use it in GitHub Desktop.
Save lfalanga/0ccb2bf7d4c5a9f44cc660dc0517c0a1 to your computer and use it in GitHub Desktop.
# Example 1: Simple while loop
count = 0
if count < 5:
print "Hello, I am an if statement and count is", count
while count <= 9:
print "Hello, I am a while and count is", count
count += 1
# Example 2: Another simple while loop
loop_condition = True
while loop_condition:
print "I am a loop"
loop_condition = False
# Example 3: Just another example
num = 1
while num <= 10: # Fill in the condition
# Print num squared
print num ** 2
# Increment num (make sure to do this!)
num += 1
# Example 4: Using while loop to prevent unexpected input
choice = raw_input('Enjoying the curse? (y/n): ').lower()
while not ((choice == 'y') or (choice == 'n')): # Fill in the condition (before the colon)
choice = raw_input("Sorry, I didn't catch that. Enter again: ").lower()
# Example 5: Infinite loop and exiting it with a break
# The difference here is that this loop is guaranteed to run at least once
# Do While alternative
count = 0
while True:
print count
count += 1
if count >= 10:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment