|
List Info
Thread: Program for outputing the letter backward
|
|
| Program for outputing the letter
backward |

|
2006-03-29 06:10:02 |
On 29/03/06, Hoffmann <oasf2004 yahoo.com> wrote:
> Hi John,
>
> (1) vehicle[index] is: 'c'
> (2) If index = index = 1, so vehicle[index] becomes:
> 'a'
What I'm getting at here is that, by changing index, we can
change
which letter we are looking at. And index is a number,
which means
it's easier to reason about than letters are.
Let's have a look at a possible solution:
>>> vehicle = 'car'
>>> index = 2
>>> print vehicle[index]
r
>>> index = 1
>>> print vehicle[index]
a
>>> index = 0
>>> print vehicle[index]
c
Notice that the three print statements are identical. That
suggests
we could write the code in a loop, with 'print
vehicle[index]' in the
body of the loop. Can you have a go at that?
--
John.
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward - almost there! |

|
2006-03-29 15:21:52 |
--- John Fouhy <john fouhy.net> wrote:
> On 29/03/06, Hoffmann <oasf2004 yahoo.com> wrote:
> > Hi John,
> >
> > (1) vehicle[index] is: 'c'
> > (2) If index = index = 1, so vehicle[index]
> becomes:
> > 'a'
>
> What I'm getting at here is that, by changing index,
> we can change
> which letter we are looking at. And index is a
> number, which means
> it's easier to reason about than letters are.
>
> Let's have a look at a possible solution:
>
> >>> vehicle = 'car'
> >>> index = 2
> >>> print vehicle[index]
> r
> >>> index = 1
> >>> print vehicle[index]
> a
> >>> index = 0
> >>> print vehicle[index]
> c
>
> Notice that the three print statements are
> identical. That suggests
> we could write the code in a loop, with 'print
> vehicle[index]' in the
> body of the loop. Can you have a go at that?
>
> --
> John.
> _______________________________________________
Hi John,
We are almost there. I changed the code and, at least,
I got the correct output. However, I also got a
traceback. I didn't understand the traceback. Could
you clarify that?
Thanks,
Hoffmann
ps: The new code:
>>> vehicle='car'
>>> index = -1 #index of the last letter
>>> lenght = len(vehicle)
>>> last = vehicle[lenght-1]
>>>
>>> while last >= vehicle[0]:
letter=vehicle[index]
print letter
index -= 1
r
a
c
Traceback (most recent call last):
File "<pyshell#40>", line 2, in
-toplevel-
letter=vehicle[index]
IndexError: string index out of range
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection
around
http://mail.yahoo.com
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward - almost there! |

|
2006-03-29 21:29:27 |
I just wanted to throw in a couple of ideas for you on this
subject.
These are the ways I would personally think of going about
this
problem:
>>> ''.join([s[n] for n in range(len(s)-1, -1,
-1)])
'rac'
Which looks a bit fugly but is nice and short if you can
manage list comps.
and now my favourite:
>>> l = list("car")
>>> l.reverse()
>>> ''.join(l)
'rac'
hth
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward - almost there! |

|
2006-03-29 23:22:00 |
--- Adam <adam.jtm30 gmail.com> wrote:
> I just wanted to throw in a couple of ideas for you
> on this subject.
> These are the ways I would personally think of going
> about this
> problem:
>
> >>> ''.join([s[n] for n in range(len(s)-1,
-1, -1)])
> 'rac'
> Which looks a bit fugly but is nice and short if you
> can manage list comps.
>
> and now my favourite:
>
> >>> l = list("car")
> >>> l.reverse()
> >>> ''.join(l)
> 'rac'
>
> hth
>
Hi Adam,
Defenitely your second alternative is really great!
Regarding that my 'bad' alternative, do you have any
suggestion about that traceback? I not only would like
to have the exercise done. And after your nice
suggestion, I ALREADY have it, but also I would like
to learn about that traceback I got previously.
Thanks,
Hoffmann
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection
around
http://mail.yahoo.com
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward - almost there! |

|
2006-03-29 23:39:57 |
> Hi John,
>
> We are almost there. I changed the code and, at least,
> I got the correct output. However, I also got a
> traceback. I didn't understand the traceback. Could
> you clarify that?
> Thanks,
> Hoffmann
> ps: The new code:
>
> >>> vehicle='car'
> >>> index = -1 #index of the last letter
> >>> lenght = len(vehicle)
> >>> last = vehicle[lenght-1]
> >>>
> >>> while last >= vehicle[0]:
> letter=vehicle[index]
> print letter
> index -= 1
>
>
> r
> a
> c
>
> Traceback (most recent call last):
> File "<pyshell#40>", line 2, in
-toplevel-
> letter=vehicle[index]
> IndexError: string index out of range
while last >= vehicle[0]:
The problem is is that neither vehicle[0] nor last change
during the
loop so it is always satisified and index eventually becomes
a number
that doesn't correspond to an index of the string.
I would suggest something along these lines instead:
for i in range(len(vehicle)-1, -1, -1):
print vehicle[i]
which is basically what my list comp did but printing out
the letters
rather than returning a list
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward - almost there! |

|
2006-03-30 00:25:09 |
Hoffmann wrote:
> We are almost there. I changed the code and, at least,
> I got the correct output. However, I also got a
> traceback. I didn't understand the traceback. Could
> you clarify that?
> Thanks,
> Hoffmann
> ps: The new code:
>
>
>>>>vehicle='car'
>>>>index = -1 #index of the last letter
>>>>lenght = len(vehicle)
>>>>last = vehicle[lenght-1]
>>>>
>>>>while last >= vehicle[0]:
>
> letter=vehicle[index]
> print letter
> index -= 1
You are still confusing the index of a letter and the letter
itself.
In [1]: vehicle = 'car'
In [2]: last = vehicle[-1]
In [3]: last
Out[3]: 'r'
last is a letter, not a number.
In [5]: vehicle[0]
Out[5]: 'c'
vehicle[0] is also a letter. So when you write
while last >= vehicle[0]:
you are comparing two characters, which is not really
helpful in the
current context. What you really want to do is compare the
index of the
current character with 0. Here is a working version in the
same style:
In [6]: index = len(vehicle)-1
In [7]: while index >= 0:
...: print vehicle[index]
...: index -= 1
...:
...:
r
a
c
The best way to reverse a string is with a slice and
negative index:
In [8]: vehicle[::-1]
Out[8]: 'rac'
but I'm going to have to leave explanation of that to
another day or
another poster.
Kent
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Program for outputing the letter
backward |

|
2006-03-30 01:14:20 |
--- Kent Johnson <kent37 tds.net> wrote:
> Hoffmann wrote:
> > We are almost there. I changed the code and, at
> least,
> > I got the correct output. However, I also got a
> > traceback. I didn't understand the traceback.
> Could
> > you clarify that?
> > Thanks,
> > Hoffmann
> > ps: The new code:
> >
> >
> >>>>vehicle='car'
> >>>>index = -1 #index of the last letter
> >>>>lenght = len(vehicle)
> >>>>last = vehicle[lenght-1]
> >>>>
> >>>>while last >= vehicle[0]:
> >
> > letter=vehicle[index]
> > print letter
> > index -= 1
>
> You are still confusing the index of a letter and
> the letter itself.
>
> In [1]: vehicle = 'car'
>
> In [2]: last = vehicle[-1]
>
> In [3]: last
> Out[3]: 'r'
>
> last is a letter, not a number.
>
> In [5]: vehicle[0]
> Out[5]: 'c'
>
> vehicle[0] is also a letter. So when you write
> while last >= vehicle[0]:
> you are comparing two characters, which is not
> really helpful in the
> current context. What you really want to do is
> compare the index of the
> current character with 0. Here is a working version
> in the same style:
>
> In [6]: index = len(vehicle)-1
>
> In [7]: while index >= 0:
> ...: print vehicle[index]
> ...: index -= 1
> ...:
> ...:
> r
> a
> c
>
> The best way to reverse a string is with a slice and
> negative index:
>
> In [8]: vehicle[::-1]
> Out[8]: 'rac'
>
> but I'm going to have to leave explanation of that
> to another day or
> another poster.
>
> Kent
>
>
> _______________________________________________
Hello Guys,
Thank you very much all of you (in special: Kent,
John, and Adam), for the nice explanations about my
excercise. I am a newbie that is studying Python
programming by myself. I appreciated your attention.
See you on my next post
Best,
Hoffmann
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection
around
http://mail.yahoo.com
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
[1-7]
|
|