Python – Loops

I While Loops

i. While you’re here 1/19

count = 0

if count < 10:
    print "Hello, I am an if statement and count is", count
    
while count < 10:
    print "Hello, I am a while and count is", count
    count += 1

ii. Condition 2/19

loop_condition = True

while loop_condition:
    print "I am a loop"
    loop_condition = False

iii. While you’re at it 3/19

num = 1
while num < 11: 
    print num **2
    num += 1
    # Fill in the condition
    # Print num squared
    # Increment num (make sure to do this!)

iv. Simple errors 4/19

choice = raw_input('Enjoying the course? (y/n)')

while choice != 'y' and choice != 'n' :
    # Fill in the condition (before the colon)
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")

v. Infinite Loops 5/19

count = 0

while count < 10:
    # Add a colon
    print count
    count += 1
    # Increment count

vi. Break 6/19

count = 0

while True:
    print count
    count += 1
    if count >= 10:
        break

vii. While/Else 7/19

import random

print "Lucky Numbers! 3 numbers will be generated."
print "If one of them is a '5', you lose!"

count = 0
while count < 3:
    num = random.randint(1, 6)
    print num
    if num == 5:
        print "Sorry, you lose!"
        break
    count += 1
else:
    print "You win!"

viii. Your own While/Else 8/19

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!

while guesses_left > 0:
    guess = int(raw_input('Your guess: '))
    if guess == random_number :
        print 'You win!'
        break
    guesses_left -= 1
else:
    print 'You lose.'

II For Loops

i. For your health 9/19

print "Counting..."

for i in range(20):
    print i

ii. For your hobbies 10/19

hobbies = []

# Add your code below!
for i in range(3):
        hobby = raw_input('What are your hobbies?: ')
        hobbies.append(hobby)
print hobbies

iii. For your strings 11/19

thing = "spam!"

for c in thing:
    print c

word = "eggs!"

# Your code here!
for d in word:
    print d

iv. For your A 12/19

phrase = "A bird in the hand..."

# Add your for loop
for char in phrase:
    if char == 'A' or char == 'a':
        print 'X',
    else:
        print char,

#Don't delete this print statement!
print

v. For your lists 13/19

numbers  = [7, 9, 12, 54, 99]

print "This list contains: "

for num in numbers:
    print num

# Add your loop below!
for numb in numbers:
    print numb**2

III Step Up Your ‘For’s

i. Looping over a dictionary 14/19

d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
    # Your code here!
    print key, d[key] 

ii. Counting as you go 15/19

choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
    print index + 1, item

iii. Multiple Lists 16/19

list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b):
    # Add your code here!
    if a>b:
        print a
    else:
        print b
    

iv. For / else 17/19
>Save and Submit

v. Change it up 18/19

fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'
for f in fruits:
    if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is.)
    print 'A', f
else:
    print 'A fine selection of fruits!'

vi. Create your own 19/19

apple = ['seeds', 'peel','pulp']
for i in apple:
    if i == 'seeds':
        print "Throw away"
    else:
        print 'You can eat it'
    
Advertisements

One thought on “Python – Loops

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s