Thursday, January 19, 2012

csci133c2.py

After we learn how to write a loop, we can do more by combining two or more of them. It is actually very easy too. Say I want to list all the possible combination of ice cream. I want to create something that will output this.
# Output desired
I like Vanilla ice cream with Gummy Bear
I like Vanilla ice cream with Cookies
I like Vanilla ice cream with Nuts
I like Vanilla ice cream with Chips
I like Chocolate ice cream with Gummy Bear
I like Chocolate ice cream with Cookies
I like Chocolate ice cream with Nuts
I like Chocolate ice cream with Chips
There are 2 kinds of ice cream and 4 kinds of topping in this program, so we most likely going to need 2 list, and then we loop them each through another.
# Solution code for the example above
iceCreams = ['Vanilla', 'Chocolate']
toppings = ['Gummy Bear', 'Cookies', 'Nuts', 'Chips']
# Looping through the ice creams
for iceCream in iceCreams:
    # Looping through the toppings
    for topping in toppings:
        print('I like', iceCream, 'ice cream with', topping)
There you have it, it is actually very simply to check for all possible combinations of the items you have in your list, does not matter if they are food, student, car, dog, anything you want. Anyway, so now let say you only want Cookies as your topping on your ice cream, you will need to check if topping is cookies, here is how to do it in python!
# if statement example
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
prime = [1, 2, 3, 5, 7]
for number in numbers:
    # Check if the number is in the list of prime we have
    if number in prime:
       print('The number', number, 'is a prime!')
Output:
The number 1 is a prime!
The number 2 is a prime!
The number 3 is a prime!
The number 5 is a prime!
The number 7 is a prime!
Note, this is just an example to showcase the example of the if statement, in real life this is not how it is done. A much more useful example: The vowels example:
# Example problem that check for vowels
vowels = 'aeiou'
words = ['apple', 'banana', 'orange', 'grapes', 'mango']
for word in words:
    for letter in word:
            if letter in vowels:
               print(letter, 'is in the word:', word)
Output:
a is in the word: apple
e is in the word: apple
a is in the word: banana
a is in the word: banana
a is in the word: banana
o is in the word: orange
a is in the word: orange
e is in the word: orange
a is in the word: grapes
e is in the word: grapes
a is in the word: mango
o is in the word: mango
Practice Problem:
Write a program that checks the word 'google', and print what are the vowels.
Write a program that print one type of ice cream on each line
# Use this list of ice cream for the practice problem
iceCreams = ['Vanilla', 'Chocolate', 'Strawberry',
             'Neapolitan', 'Chocolate Chip', 'French vanilla', 
             'Cookies and Cream', 'Vanilla Fudge Ripple', 'Praline pecan']
Write a program that print 'I love ice cream' 50 times :)
# Tip: Don't write print() 16 times, think about loop, n * n = 16...?
Write a program that count down from 20 to 1
# Tip: Create an integer counter, and -1 each time you run through
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
Write a program that check the vowel in the ice cream name list
# Use this list of ice cream for the practice problem
iceCreams = ['Vanilla', 'Chocolate']
Solution
# Solution on the question
iceCreams = ['Vanilla', 'Chocolate']
vowels = 'aeiou'
for iceCream in iceCreams:
    for letter in iceCream:
        for letter in vowels:
            print(iceCream, 'has vowel', letter, 'in it')

No comments:

Post a Comment