Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77359 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-08-31 17:02 -0400 |
| Last post | 2014-08-31 22:52 -0600 |
| Articles | 8 on this page of 28 — 10 participants |
Back to article view | Back to comp.lang.python
This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 17:02 -0400
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 17:07 -0400
Re: This could be an interesting error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-31 22:38 +0100
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 18:04 -0400
Re: This could be an interesting error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-31 23:26 +0100
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 18:42 -0400
Re: This could be an interesting error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-01 00:21 +0100
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 20:08 -0400
Re: This could be an interesting error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-01 01:56 +0100
Re: This could be an interesting error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-01 12:53 +1000
Re: This could be an interesting error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-01 07:34 +0100
Re: This could be an interesting error Ned Batchelder <ned@nedbatchelder.com> - 2014-08-31 22:12 -0400
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 22:54 -0400
Re: This could be an interesting error Larry Hudson <orgnut@yahoo.com> - 2014-08-31 21:55 -0700
Re: This could be an interesting error Chris Angelico <rosuav@gmail.com> - 2014-09-01 15:12 +1000
Re: This could be an interesting error Rustom Mody <rustompmody@gmail.com> - 2014-08-31 23:53 -0700
Re: This could be an interesting error MRAB <python@mrabarnett.plus.com> - 2014-08-31 22:53 +0100
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 18:07 -0400
Re: This could be an interesting error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-01 12:12 +1000
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 22:13 -0400
Re: This could be an interesting error Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-09-01 19:25 +1200
Re: This could be an interesting error Michael Torrie <torriem@gmail.com> - 2014-08-31 16:10 -0600
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 18:31 -0400
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 20:04 -0400
Re: This could be an interesting error MRAB <python@mrabarnett.plus.com> - 2014-09-01 01:23 +0100
Re: This could be an interesting error Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-31 20:35 -0400
Re: This could be an interesting error Michael Torrie <torriem@gmail.com> - 2014-08-31 22:15 -0600
Re: This could be an interesting error Michael Torrie <torriem@gmail.com> - 2014-08-31 22:52 -0600
Page 2 of 2 — ← Prev page 1 [2]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2014-09-01 19:25 +1200 |
| Message-ID | <c6ilbdFqim5U1@mid.individual.net> |
| In reply to | #77382 |
Steven D'Aprano wrote: > or words in Foreign like "cwm" Seeing that "w" is a vowel in Welsh, there should probably be a special version of the program for Welsh speakers. (Welshlatin? Pigwelsh?) -- Greg
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-08-31 16:10 -0600 |
| Message-ID | <mailman.13668.1409523047.18130.python-list@python.org> |
| In reply to | #77359 |
On 08/31/2014 03:02 PM, Seymore4Head wrote:
> def pigword(test):
> for x in range(len(test)):
> if test[x] in "AEIOUaeiou":
> stem = test [x:]
> prefix = test [:x]
> pigword = stem + prefix + "ay"
> print ("Stem ",stem)
> print ("Prefix",prefix)
> print (pigword)
> break
> return (pigword)
So, what do you think will happen if the word contains no vowels? Where
is pigword defined?
> for x in range(len(newex)):
> sentence = sentence + pigword(newex[x])+ " "
> print (sentence)
> wait = input (" Wait")
You don't need to iterate over range(len(blah)). The standard idiom
when you need index as well as the item itself is to iterate over
enumerate(). Or if you don't need the index, just iterate directly.
You can iterate directly over the list, or the letters in the word,
optionally getting an index. It's much cleaner and less error prone.
Consider something like:
def pigword(word):
for x,letter in enumerate(word):
# x is index (position), letter is the value at that index
if letter in "AEIOUaeiou":
...
for word in list_of_words:
sentence = sentence + pigword(word) + " "
...
That doesn't solve your little logic problem, though I think you can
figure that part out easily!
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-31 18:31 -0400 |
| Message-ID | <ai870a9bene7th3323i8k714n4hsueq1er@4ax.com> |
| In reply to | #77364 |
On Sun, 31 Aug 2014 16:10:27 -0600, Michael Torrie <torriem@gmail.com>
wrote:
>On 08/31/2014 03:02 PM, Seymore4Head wrote:
>> def pigword(test):
>> for x in range(len(test)):
>> if test[x] in "AEIOUaeiou":
>> stem = test [x:]
>> prefix = test [:x]
>> pigword = stem + prefix + "ay"
>> print ("Stem ",stem)
>> print ("Prefix",prefix)
>> print (pigword)
>> break
>> return (pigword)
>
>So, what do you think will happen if the word contains no vowels? Where
>is pigword defined?
>
>> for x in range(len(newex)):
>> sentence = sentence + pigword(newex[x])+ " "
>> print (sentence)
>> wait = input (" Wait")
>
>You don't need to iterate over range(len(blah)). The standard idiom
>when you need index as well as the item itself is to iterate over
>enumerate(). Or if you don't need the index, just iterate directly.
>You can iterate directly over the list, or the letters in the word,
>optionally getting an index. It's much cleaner and less error prone.
>Consider something like:
>
>def pigword(word):
> for x,letter in enumerate(word):
> # x is index (position), letter is the value at that index
> if letter in "AEIOUaeiou":
> ...
>
>for word in list_of_words:
> sentence = sentence + pigword(word) + " "
> ...
>
>That doesn't solve your little logic problem, though I think you can
>figure that part out easily!
>
Thanks I will give that a try.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-31 20:04 -0400 |
| Message-ID | <2jc70at4qcpqmbes72r2f0tsjk8up0j6mn@4ax.com> |
| In reply to | #77364 |
On Sun, 31 Aug 2014 16:10:27 -0600, Michael Torrie <torriem@gmail.com>
wrote:
>On 08/31/2014 03:02 PM, Seymore4Head wrote:
>> def pigword(test):
>> for x in range(len(test)):
>> if test[x] in "AEIOUaeiou":
>> stem = test [x:]
>> prefix = test [:x]
>> pigword = stem + prefix + "ay"
>> print ("Stem ",stem)
>> print ("Prefix",prefix)
>> print (pigword)
>> break
>> return (pigword)
>
>So, what do you think will happen if the word contains no vowels? Where
>is pigword defined?
>
>> for x in range(len(newex)):
>> sentence = sentence + pigword(newex[x])+ " "
>> print (sentence)
>> wait = input (" Wait")
>
>You don't need to iterate over range(len(blah)). The standard idiom
>when you need index as well as the item itself is to iterate over
>enumerate(). Or if you don't need the index, just iterate directly.
>You can iterate directly over the list, or the letters in the word,
>optionally getting an index. It's much cleaner and less error prone.
>Consider something like:
>
>def pigword(word):
> for x,letter in enumerate(word):
> # x is index (position), letter is the value at that index
> if letter in "AEIOUaeiou":
> ...
>
>for word in list_of_words:
> sentence = sentence + pigword(word) + " "
> ...
>
>That doesn't solve your little logic problem, though I think you can
>figure that part out easily!
>
I am still kind of shooting in the dark.
I wanted to try your example and it doesn't seem to work.
This is the latest version of changes I have made so the entire
program looks like this:
newex='Hey buddy get away from my car'
newex = newex.split()
sentence=""
print (newex)
wait = input (" Wait")
def pigword(test):
for x in range(len(test)):
if test[x] in "AEIOUYyaeiou":
stem = test [x:]
prefix = test [:x]
pigword = stem + prefix + "ay"
print ("Stem ",stem)
print ("Prefix",prefix)
print (pigword)
break
return (pigword)
for x in range(len(newex)):
sentence = sentence + pigword(newex[x])+ " "
print (sentence)
wait = input (" Wait")
Trying to use your example
>def pigword(word):
> for x,letter in enumerate(word):
> # x is index (position), letter is the value at that index
> if letter in "AEIOUaeiou":
BTW I added "AEIOUYyaeiou" y as a vowel.
I tried changing:
for x in range(len(test)):
to
for x in enumerate(test):
That causes an error to popup in a different place. I don't
understand why.
Traceback (most recent call last):
File "C:\Functions\piglatin.py", line 26, in <module>
sentence = sentence + pigword(newex[x])+ " "
File "C:\Functions\piglatin.py", line 15, in pigword
if test[x] in "AEIOUYyaeiou":
TypeError: string indices must be integers
Since you included:
>for word in list_of_words:
> sentence = sentence + pigword(word) + " "
I take it you anticipated a fault here. I don't understand why.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-09-01 01:23 +0100 |
| Message-ID | <mailman.13672.1409531019.18130.python-list@python.org> |
| In reply to | #77374 |
On 2014-09-01 01:04, Seymore4Head wrote:
> On Sun, 31 Aug 2014 16:10:27 -0600, Michael Torrie <torriem@gmail.com>
> wrote:
>
>>On 08/31/2014 03:02 PM, Seymore4Head wrote:
>>> def pigword(test):
>>> for x in range(len(test)):
>>> if test[x] in "AEIOUaeiou":
>>> stem = test [x:]
>>> prefix = test [:x]
>>> pigword = stem + prefix + "ay"
>>> print ("Stem ",stem)
>>> print ("Prefix",prefix)
>>> print (pigword)
>>> break
>>> return (pigword)
>>
>>So, what do you think will happen if the word contains no vowels? Where
>>is pigword defined?
>>
>>> for x in range(len(newex)):
>>> sentence = sentence + pigword(newex[x])+ " "
>>> print (sentence)
>>> wait = input (" Wait")
>>
>>You don't need to iterate over range(len(blah)). The standard idiom
>>when you need index as well as the item itself is to iterate over
>>enumerate(). Or if you don't need the index, just iterate directly.
>>You can iterate directly over the list, or the letters in the word,
>>optionally getting an index. It's much cleaner and less error prone.
>>Consider something like:
>>
>>def pigword(word):
>> for x,letter in enumerate(word):
>> # x is index (position), letter is the value at that index
>> if letter in "AEIOUaeiou":
>> ...
>>
>>for word in list_of_words:
>> sentence = sentence + pigword(word) + " "
>> ...
>>
>>That doesn't solve your little logic problem, though I think you can
>>figure that part out easily!
>>
> I am still kind of shooting in the dark.
>
> I wanted to try your example and it doesn't seem to work.
> This is the latest version of changes I have made so the entire
> program looks like this:
>
> newex='Hey buddy get away from my car'
> newex = newex.split()
> sentence=""
>
> print (newex)
> wait = input (" Wait")
>
> def pigword(test):
> for x in range(len(test)):
> if test[x] in "AEIOUYyaeiou":
> stem = test [x:]
> prefix = test [:x]
> pigword = stem + prefix + "ay"
> print ("Stem ",stem)
> print ("Prefix",prefix)
> print (pigword)
> break
> return (pigword)
>
> for x in range(len(newex)):
> sentence = sentence + pigword(newex[x])+ " "
> print (sentence)
> wait = input (" Wait")
>
> Trying to use your example
>>def pigword(word):
>> for x,letter in enumerate(word):
>> # x is index (position), letter is the value at that index
>> if letter in "AEIOUaeiou":
> BTW I added "AEIOUYyaeiou" y as a vowel.
>
> I tried changing:
> for x in range(len(test)):
> to
> for x in enumerate(test):
>
> That causes an error to popup in a different place. I don't
> understand why.
> Traceback (most recent call last):
> File "C:\Functions\piglatin.py", line 26, in <module>
> sentence = sentence + pigword(newex[x])+ " "
> File "C:\Functions\piglatin.py", line 15, in pigword
> if test[x] in "AEIOUYyaeiou":
> TypeError: string indices must be integers
>
Try printing out x. That should give you a clue!
> Since you included:
>>for word in list_of_words:
>> sentence = sentence + pigword(word) + " "
> I take it you anticipated a fault here. I don't understand why.
>
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-31 20:35 -0400 |
| Message-ID | <qjf70a9nqiq70f6buqc3rl2gulbknejhvl@4ax.com> |
| In reply to | #77377 |
On Mon, 01 Sep 2014 01:23:36 +0100, MRAB <python@mrabarnett.plus.com>
wrote:
>On 2014-09-01 01:04, Seymore4Head wrote:
>> On Sun, 31 Aug 2014 16:10:27 -0600, Michael Torrie <torriem@gmail.com>
>> wrote:
>>
>>>On 08/31/2014 03:02 PM, Seymore4Head wrote:
>>>> def pigword(test):
>>>> for x in range(len(test)):
>>>> if test[x] in "AEIOUaeiou":
>>>> stem = test [x:]
>>>> prefix = test [:x]
>>>> pigword = stem + prefix + "ay"
>>>> print ("Stem ",stem)
>>>> print ("Prefix",prefix)
>>>> print (pigword)
>>>> break
>>>> return (pigword)
>>>
>>>So, what do you think will happen if the word contains no vowels? Where
>>>is pigword defined?
>>>
>>>> for x in range(len(newex)):
>>>> sentence = sentence + pigword(newex[x])+ " "
>>>> print (sentence)
>>>> wait = input (" Wait")
>>>
>>>You don't need to iterate over range(len(blah)). The standard idiom
>>>when you need index as well as the item itself is to iterate over
>>>enumerate(). Or if you don't need the index, just iterate directly.
>>>You can iterate directly over the list, or the letters in the word,
>>>optionally getting an index. It's much cleaner and less error prone.
>>>Consider something like:
>>>
>>>def pigword(word):
>>> for x,letter in enumerate(word):
>>> # x is index (position), letter is the value at that index
>>> if letter in "AEIOUaeiou":
>>> ...
>>>
>>>for word in list_of_words:
>>> sentence = sentence + pigword(word) + " "
>>> ...
>>>
>>>That doesn't solve your little logic problem, though I think you can
>>>figure that part out easily!
>>>
>> I am still kind of shooting in the dark.
>>
>> I wanted to try your example and it doesn't seem to work.
>> This is the latest version of changes I have made so the entire
>> program looks like this:
>>
>> newex='Hey buddy get away from my car'
>> newex = newex.split()
>> sentence=""
>>
>> print (newex)
>> wait = input (" Wait")
>>
>> def pigword(test):
>> for x in range(len(test)):
>> if test[x] in "AEIOUYyaeiou":
>> stem = test [x:]
>> prefix = test [:x]
>> pigword = stem + prefix + "ay"
>> print ("Stem ",stem)
>> print ("Prefix",prefix)
>> print (pigword)
>> break
>> return (pigword)
>>
>> for x in range(len(newex)):
>> sentence = sentence + pigword(newex[x])+ " "
>> print (sentence)
>> wait = input (" Wait")
>>
>> Trying to use your example
>>>def pigword(word):
>>> for x,letter in enumerate(word):
>>> # x is index (position), letter is the value at that index
>>> if letter in "AEIOUaeiou":
>> BTW I added "AEIOUYyaeiou" y as a vowel.
>>
>> I tried changing:
>> for x in range(len(test)):
>> to
>> for x in enumerate(test):
>>
>> That causes an error to popup in a different place. I don't
>> understand why.
>> Traceback (most recent call last):
>> File "C:\Functions\piglatin.py", line 26, in <module>
>> sentence = sentence + pigword(newex[x])+ " "
>> File "C:\Functions\piglatin.py", line 15, in pigword
>> if test[x] in "AEIOUYyaeiou":
>> TypeError: string indices must be integers
>>
>Try printing out x. That should give you a clue!
>
>> Since you included:
>>>for word in list_of_words:
>>> sentence = sentence + pigword(word) + " "
>> I take it you anticipated a fault here. I don't understand why.
>>
This might be a time saver later on, but I don't think I am ready for
changing to enumerate yet.
I did Google it. :)
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-08-31 22:15 -0600 |
| Message-ID | <mailman.13679.1409544968.18130.python-list@python.org> |
| In reply to | #77374 |
On 08/31/2014 06:04 PM, Seymore4Head wrote:
>> for x,letter in enumerate(word):
>> # x is index (position), letter is the value at that index
>> if letter in "AEIOUaeiou":
> I tried changing:
> for x in range(len(test)):
> to
> for x in enumerate(test):
Read my example again. You missed something vital. enumerate returns
both a position and the item:
for x,letter in "hello":
print (x, " ", letter)
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-08-31 22:52 -0600 |
| Message-ID | <mailman.13680.1409547142.18130.python-list@python.org> |
| In reply to | #77374 |
On 08/31/2014 10:15 PM, Michael Torrie wrote:
> On 08/31/2014 06:04 PM, Seymore4Head wrote:
>>> for x,letter in enumerate(word):
>>> # x is index (position), letter is the value at that index
>>> if letter in "AEIOUaeiou":
>> I tried changing:
>> for x in range(len(test)):
>> to
>> for x in enumerate(test):
>
> Read my example again. You missed something vital. enumerate returns
> both a position and the item:
>
Sigh. Oops. Make that:
for x,letter in enumerate("hello"):
print (x, " ", letter)
You definitely should start doing it this way. It's more "pythonic" and
also cleaner and easier to understand when you're reading the code later.
Also you did add Y to your list of vowels, but what about words that
have no vowels and no Y? Maybe not real words, but things that might be
likely to be in sentences: "I am from Washington, DC"
It's way better to fix your logic so you have a fall-back. Hint: Set
pigword before your enter the loop so that it always contains
_something_, preferably the original word!
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web