MrSchmid.com

For Loops, Slicing and Tuples

Posted On: Mon, 2008-04-21 14:47 by mrschmid

Invinite Loop For Loops:

While loops continue until a condition is met. For loops continue through a sequence. For each item or step the for loop will continue. For loops can replace those while counting loops that we had used before.

Loopy String

Counter Program

This counter program uses the for loop with the range() function. The range function returns a range of numbers from the starting value to the ending value. A step number can be used that will count by that number (fives for example). Range(starting value, ending value, step by value)

Click Read More for the rest...

 

Message analyzer

The message analyzer uses the len() function to measure the length of a string. Len counts every character in a string, including punctuation and spacing.

String immutability

Immutability is just a fancy word for “unchanging”. Strings are not able to be changed. We can create new strings from previous strings, but we can’t modify a string. Some of the following programs will demonstrate how we can manipulate immutable strings.

No Vowels

No vowels uses a constant value. Constant values in python are traditionally named in all caps. This is simply a naming convention, and does not have any effect on how Python interprets the program.

Pizza Slicer

Slicing allows us to use part of a string by specifying a starting and ending point. In the program you’ll see something like word[0:5]. This command will return the slice from starting from 0 to 5. If we are starting at 0, we can use some short hand and just use word[:0]. Word[:] will return the entire whole slice.

 

Tuples – Hero Inventory

Tuples are a group of variables or strings that can be used in a number of ways. In Hero Inventory, you’ll use a tuple to store the inventory of our trusty hero. It’s good form to use a format that lists each item in a tuple down the screen. Tuples can be sliced just like strings.

Word Jumble

Follow through the comments as you create the word jumble game. Replace the possible words with words of your own choosing for your version of the program.

Exercises:

New Counter (Users specifies starting and ending numbers)

Write a program that will count from a starting point and ending point that a user specifies. Also give the user the option to count by a number (any number they choose).

Pig Latin Converter

Write a program that will take any text the user inputs and return the pig latin translation. For this converter, use the following (slightly modified) definition of pig latin:

- If the word starts in a consonant, move the consonant to the end of the word and add “ay”. Example: “spam” becomes “pamsay”

- If the word starts in a vowel, just add “ay” to the end of the word. Example: “angry” becomes “angryay”.

- Don’t be concerned with one syllable words for this converter, just use the same rules as above.

Practices: LoopyString, Counter, Message Analyzer, NoVowels, PizzaSlicer, HeroInventory, WordJumbler
Exercises: NewCounter, PigLatinConverter

AttachmentSize
loopingstring.gif4.35 KB
MessageAnalyzer.gif5.95 KB
novowels.gif10.26 KB
pizzaslicer.gif10.91 KB
hero-inventory.gif14.72 KB
WordScrambler.gif15.69 KB
counter2.gif6.61 KB
( categories: | )