Codecademy Python Answers Loops

codecademy-loops-answers

Loops 1/19 – While You’re Here

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

Loops 2/19 – Condition

loop_condition = True

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

Loops 3/19 – While You’re at it

num = 1

while num <= 10:
    print num ** 2
    num +=1
    
    

Loops 4/19 – Simple Errors

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: ")

Loops 5/19 – Infinite Loops

count = 0

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

Loops 6/19 – Break

count = 0

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

Loops 7/19 – While / Else

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!"

Loops 8/19 – Your Own While/Else

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("Enter guess number"))
    if guess == random_number:
        print "You win!"
        break
    guesses_left -= 1
else:
        print "You lose."
    
        

Loops 9/19 – For Your Health

print "Counting..."

for i in range(20):
    print i
    
    

Loops 10/19 – For Your Hobbies

hobbies = []

for i in range(3):
    hobby = raw_input("An activity you enjoy: ")
    hobbies.append(hobby)
print hobbies

Loops 11/19 – For Your Strings

thing = "spam!"

for c in thing:
    print c

word = "eggs!"
for c in word:
    print c

# Your code here!

Loops 12/19 – For Your A

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

Loops 13/19 – For Your Lists

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

print "This list contains: "

for num in numbers:
    print num

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

Loops 14/19 – Looping Over a Dictionary

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

for key in d:
   for key in range(0, 1):
    if d["a"] == "apple" or d["b"] == "berry" or d["c"] == "cherry":
        print "a", d["a"]
        print "b", d["b"]
        print "c", d["c"]
print greeting, name

Loops 15/19 – Counting as you go

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

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

Loops 16/19 – Multiple Lists

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):
    if a>b:
        print a
    else:
        print b
    # Add your code here!
    

Loops 17/19 – For/Else

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.)
        break
    print 'A', f
else:
    print 'A fine selection of fruits!'

Loops 18/19 – Changing It Up

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!'

Loops 19/19 – Create Your Own

prathap = "10"
for i in range(int(prathap)):
    print i
else:
    print prathap

Leave a Reply

Your email address will not be published. Required fields are marked *