#!/usr/bin/env python # coding: utf-8 # # Chapter 14 Loops # # # In[1]: # 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 # In[2]: # 02 Condition loop_condition = True while loop_condition: print ("I am a loop") loop_condition = False # In[3]: # 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!) # In[4]: # 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: ") # In[5]: # 05 Infinite loops count = 0 while count < 10: # Add a colon print (count) count += 1# Increment count # In[6]: # 06 Break count = 0 while True: print (count) count += 1 if count >= 10: break # In[7]: # 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!") # In[8]: # 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!") # In[9]: # 09 For your health print ("Counting...") for i in range(20): print (i) # In[10]: # 10 For your hobbies hobbies = [] # Add your code below! for i in range(3): hobby = input("What's your hobby?") hobbies.append(hobby) # In[11]: # 11 For your strings thing = "spam!" for c in thing: print (c) word = "eggs!" # Your code here! for c in word: print (c) # In[12]: # 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 # In[13]: # 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) # In[14]: # 14 Looping over a dictionary d = {'x': 9, 'y': 10, 'z': 20} for key in d: print ( key, d[key] )# Your code here! # In[15]: # 15 Counting as you go choices = ['pizza', 'pasta', 'salad', 'nachos'] print ('Your choices are:') for index, item in enumerate(choices): print (index + 1, item) # In[16]: # 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)) # In[17]: # 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!') # In[18]: # 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!') # In[19]: # 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~")