Python program question?

Started by
7 comments, last by guzumba 16 years, 2 months ago
I was wondering if anyone could give me some advice on making a short program that would change regular English into Pig Latin. If you dont know what pig latin is here is a brief description.
Pig-Latin: is a kind of ``jive talk'' that was popular in the 1930s and 1940s. An English word is translated into Pig-Latin by moving all its leading consonants to the end of the word and attaching ``ay'' to the end. 

For example, 
My dog has fleas! becomes 
ymay ogday ashay easflay!
 

When a word begins with a vowel, then no leading consonants are moved, and instead, ''shay'' is attached to the end of the word. 

For example, 
I bought it on Ebay, yesterday. becomes 
ishay oughtbay itshay onshay ebayshay, esterdayyay.


I can figure out how to change one word from english to pig-latin but not a whole sentence. Here is the code for one word.

vowel = "aeiou"
word = raw_input("Type a word and press Enter: ")

if word[0] in vowel:
    print word[0:] + "shay"
else:
    print word[1:] + word[0] + "ay"
raw_input("\nPress Enter to Exit")


Can someone please help me please. Im stuck and my head is about to explode! Thanks Everyone Andrew
Advertisement
Two questions:

1. How is Torque relevant here?
2. Are you familiar with loops? Specifically the for loop?
Quote:Original post by oler1s
Two questions:

1. How is Torque relevant here?
2. Are you familiar with loops? Specifically the for loop?


Im sorry guys I didnt mean to type Torque I meant Python. And no I have not learn "for" loops yet. I am not supposed to include a "for" loop for this project. So if you could help me only with "while" loops that would be amazing.

Thanks
Andrew
You have the algorithm for one word (not including looking for "sh" "st", etc.) so you now have to iterate over the sentence and apply it to each word.

I think you can use the .split() method of a string and then a "for __ in __" do you know how to use those?
Quote:And no I have not learn "for" loops yet. I am not supposed to include a "for" loop for this project. So if you could help me only with "while" loops that would be amazing.
Well, that's a bummer. The for loop takes a special place in Python. Python treats sequences like first class citizens, and the for loop is extremely useful in iterating over a sequence.

In any case, if you want to go the while loop route. Split the string into words as Boder suggested. Each is accessed by an index, assuming you stored the words in a list. So you basically need a constantly incrementing index (let's call it i). Just use a while loop, incrementing i each time you go through the loop.

You also have the issue of boundary conditions. That is, when do you stop incrementing i? There's two ways to go about it. The traditional CS way is to have a check for i<length of whatever sequence you have.

You could do that in Python, but the more natural way is to use a try/except block. Already, this is getting more complicated than a simple for loop, so I'm not sure why your assignment has such a constraint.

Go in the order I typed this up. Split up your string into words and store the words in a list. Create an index variable. Create the format of the loop. Design the loop to increment i. Put your pig latin code into the loop, modifying it to use i. Then when it works, fix the problem with it crashing when it reaches the end of the list of words.
Thanks for the replys everyone. I kinda see what all of you are saying but can anyone give me some examples or something. It is very hard to understand what everyone is talking about without examples of some type. Imagine learning a programming language without seeing example code of some type. Could you show me what you mean by spliting strings up into words and storing those words. Im not sure if ive ever done that before.
When you call raw_input(), you get a string like: 'foo bar baz quux'. This contains several words.

The .split() function of a string gives you a list of the words in the string.

>>> 'foo bar baz quux'.split()['foo', 'bar', 'baz', 'quux']


A list comprehension lets us apply some transformation to each element of a list. We set up a temporary "index" variable, and express some function of that variable, and the function gets evaluated with each element of the list (i.e., the index assumes each value in the list.

>>> [x * 2 for x in ['foo', 'bar', 'baz', 'quux']]['foofoo', 'barbar', 'bazbaz', 'quuxquux']


Of course, anywhere we could just write a value like that, we could write a more complicated expression instead (assuming there are no side effects, such as printing):

>>> [x * 2 for x in 'foo bar baz quux'.split()]['foofoo', 'barbar', 'bazbaz', 'quuxquux']


And of course we could just take the words from raw_input() directly - there is no need to assign them to a temporary variable first:

>>> [x * 2 for x in raw_input().split()]foo bar baz quux['foofoo', 'barbar', 'bazbaz', 'quuxquux']


The .join() of a string accepts a list: the string inserts itself between elements of the list, and returns the result of stringing all of that together.

>>> ','.join(['foo', 'bar', 'baz', 'quux'])'foo,bar,baz,quux'


The 'def' keyword allows us to define our own functions, which lets us do more complicated transformations on our index when we use the list comprehension.

>>> def double_everything_except_quux(x):...   if x == 'quux': return x...   else: return x * 2...>>> [double_everything_except_quux(y) for y in 'foo bar baz quux'.split()]['foofoo', 'barbar', 'bazbaz', 'quux']


Notice that there is nothing special about the variable name 'x' or 'y', and they don't even need to match: the 'y' in our list comprehension is known as 'x' within the function.

You now know everything you need. Make a pigify() function that changes a single word, and use the above techniques to create the pigified sentence. Think about what string you will want to use for .join()ing. Notice that, again, you can use expressions where you would use values, so in particular, you can feed the list comprehension directly to .join() - it's just like providing the list that is "comprehended".
>>> example = "This is a string">>> example'This is a string'>>> for word in example.split(' '):...     print word...Output:   Thisisastring
Hi everyone! Im still having trouble making the function to change one word.
I am able to change a word starting with 1 consonant, but not a word with 2 or 3.

For example I can change
english = "hello",
into,
pig-latin = "ellohay".
but not,
english = "three"
into,
pig-latin = "eethray"

In pig-latin you are supposed to move every consonant(not just the first one)before the first vowel to the end of your word.

I have this code so far for defining my function.
vowels= "aeiou"word = raw_input("Type a word: ")def change_word_into_piglatin():    if word[0] not in vowels:        print word[1:] + word[0] + "ay"    else:        print word[0:] + "shay"print change_word_into_piglatin()raw_input("Press ENTER to exit")

This code works fine for the word "hello", but doesnt work for "three". Can someone please help me?? And for some reason it keeps printing out "None" in the output and I wasnt sure why? Could someone help me with that too?

Thanks
Andrew

This topic is closed to new replies.

Advertisement