# 01 While you are here
count = 0
if count < 5:
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
Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am a while and count is 1 Hello, I am a while and count is 2 Hello, I am a while and count is 3 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9
# 02 Condition
loop_condition = True
while loop_condition:
print ("I am a loop")
loop_condition = False
I am a loop
# 03 While you are at it
num = 1
while num <= 10: # Fill in the condition (before the colon)
print (num ** 2) # Print num squared
num += 1# Increment num (make sure to do this!)
1 4 9 16 25 36 49 64 81 100
# 04 Simple errors
choice = input('Enjoying the course? (y/n)')
while choice != "y" and choice != "n":# Fill in the condition (before the colon)
choice = input("Sorry, I didn't catch that. Enter again: ")
Enjoying the course? (y/n)y
# 05 Infinite loops
count = 0
while count < 10: # Add a colon
print (count)
count += 1# Increment count
0 1 2 3 4 5 6 7 8 9
# 06 Break
count = 0
while True:
print (count)
count += 1
if count >= 10:
break
0 1 2 3 4 5 6 7 8 9
# 07 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)
count += 1
if num == 5:
print ("Sorry, you lose!")
break
else:
print ("You win!")
Lucky Numbers! 3 numbers will be generated. If one of them is a '5', you lose! 6 5 Sorry, you lose!
# 08 Your own while else
from random import randrange
random_number = randrange(1, 10)
count = 0
# Start your game!
guess = int(input("Enter a guess:"))
while guess != random_number:
count += 1
if count == 3:
print ("OMG! You lose! the answer is", str(random_number) + ", BB!")
break
guess = int(input("Guess wrong, %s chance(s) left, enter a guess:" % (3 - count)))
else:
print ("OMG! You win! GB!")
Enter a guess:4 Guess wrong, 2 chance(s) left, enter a guess:5 Guess wrong, 1 chance(s) left, enter a guess:2 OMG! You lose! the answer is 9, BB!
# 09 For your health
print ("Counting...")
for i in range(20):
print (i)
Counting... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 10 For your hobbies
hobbies = []
# Add your code below!
for i in range(3):
hobby = input("What's your hobby?")
hobbies.append(hobby)
What's your hobby? What's your hobby?climbing What's your hobby?reading
# 11 For your strings
thing = "spam!"
for c in thing:
print (c)
word = "eggs!"
# Your code here!
for c in word:
print (c)
s p a m ! e g g s !
# 12 For your A
s = "A bird in the hand..."
# Add your for loop
for c in s:
if c == 'A' or c == 'a':
print ("X",)
else:
print (c,)
#Don't delete this print statement!
print() # fanfank thinks it's related to buffer issue, without this line, the content is left in the buffer and not printed
X b i r d i n t h e h X n d . . .
# 13 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)
This list contains: 7 9 12 54 99 49 81 144 2916 9801
# 14 Looping over a dictionary
d = {'x': 9, 'y': 10, 'z': 20}
for key in d:
print ( key, d[key] )# Your code here!
y 10 z 20 x 9
# 15 Counting as you go
choices = ['pizza', 'pasta', 'salad', 'nachos']
print ('Your choices are:')
for index, item in enumerate(choices):
print (index + 1, item)
Your choices are: 1 pizza 2 pasta 3 salad 4 nachos
# 16 Multiple lists
a = [3, 9, 17, 15, 19]
b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
# Add your code here!
for a, b in zip(a,b):
print (max(a, b))
3 9 17 15 30
# 17 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!')
You have... A banana A apple A orange A tomato is not a fruit!
# 18 Change it up
fruits = ['banana', 'apple', 'orange', '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!')
You have... A banana A apple A orange A pear A grape A fine selection of fruits!
# 19 Create your own
lst1 = [1, 3, 5, 8, 9]
lst2 = [1, 3, 5, 7, 9, 11]
for a, b in zip(lst1, lst2):
if a != b:
break;
print (a)
else:
print ("Hi, impossible to execute me~")
1 3 5