Re: [Tutor] Program for outputing the letter backward
--- Ed Singleton <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > --- John Fouhy <[EMAIL PROTECTED]> wrote: > > > > > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> > wrote: > > > > vehicle='car' > > > > index = vehicle[-1] #the last letter > > > > index_zero = vehicle[0] #the first letter > > > > > > > > while index >= index_zero: > > > >letter=vehicle[index] > > > >print letter > > > >index -= 1 > > > > > > > > The problem is that I get no output here. > Could I > > > hear > > > > from you? > > > > > > I can print the letters backwards like this: > > > > > > vehicle = 'car' > > > print vehicle[2] > > > print vehicle[1] > > > print vehicle[0] > > > > > > Output: > > > > > > r > > > a > > > c > > > > > > - > > > > > > This is not very useful, though, because it will > > > only work for strings > > > that are exactly three letters long. Can you > see > > > how to write a loop > > > to produe this output? > > > > > > Hint: the len() function will tell you how long > a > > > string is. > > > > > > eg: if vehicle == 'car' then len(vehicle) == 3. > > > > > > -- > > > John. > > > ___ > > > > Hi John, > > > > I am still 'blind' here. > > > > Please, see below what I did, and what I got: > > > > >>> vehicle='car' > > >>> index = 0 > > >>> lenght =len(vehicle) > > >>> last = vehicle[lenght -1] > > >>> while last >= vehicle[0]: > > ... letter = vehicle[index] > > ... print letter > > ... last -= 1 > > ... > > c > > Traceback (most recent call last): > > File "", line 4, in ? > > TypeError: unsupported operand type(s) for -=: > 'str' > > and 'int' > > > > As you can see, I am still a newbie... > > Could anyone, please, guide me on this exercise? > > Thanks! > > Hoffmann > > A technique I used to find useful when I was very > first learning (and > struggling) was to calculate the variables for each > pass of the loop > (basically remove all the variable names, just like > doing algebra). > > So: > > >>> vehicle='car' > >>> index = 0 > >>> lenght = len(vehicle) # therefore: > >>> lenght = 3 > >>> last = vehicle[lenght -1] # therefore: > >>> last = vehicle[2] # therefore: > >>> last = "r" > >>> while "r" >= "c": # first pass > ... letter = vehicle[index] # therefore: > ... letter = vehicle[0] # therefore: > ... letter = "c" > ... print letter > ... last -= 1 # therefore: > ... "r" -= 1 # therefore: > ... "r" = "r" - 1 # therefore: > ... ERROR > > You'll find that that can make it much clearer what > is actually > happening. An alternative is to use lots and lots > of print > statements: > > >>> vehicle='car' > >>> print vehicle > >>> index = 0 > >>> print index > >>> lenght = len(vehicle) > >>> print lenght > > and so on... > > It would be really good if there was a way to have a > "verbose" > interpreter that showed you each of the steps in the > process, but I > don't know of any. > > For example: > > >>> last = vehicle[lenght -1] > last = vehicle[2] > last = "r" > >>> > > Ed > Hi Ed, Many thanks for the hints! 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
Re: [Tutor] Program for outputing the letter backward
On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > --- John Fouhy <[EMAIL PROTECTED]> wrote: > > > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > > vehicle='car' > > > index = vehicle[-1] #the last letter > > > index_zero = vehicle[0] #the first letter > > > > > > while index >= index_zero: > > >letter=vehicle[index] > > >print letter > > >index -= 1 > > > > > > The problem is that I get no output here. Could I > > hear > > > from you? > > > > I can print the letters backwards like this: > > > > vehicle = 'car' > > print vehicle[2] > > print vehicle[1] > > print vehicle[0] > > > > Output: > > > > r > > a > > c > > > > - > > > > This is not very useful, though, because it will > > only work for strings > > that are exactly three letters long. Can you see > > how to write a loop > > to produe this output? > > > > Hint: the len() function will tell you how long a > > string is. > > > > eg: if vehicle == 'car' then len(vehicle) == 3. > > > > -- > > John. > > ___ > > Hi John, > > I am still 'blind' here. > > Please, see below what I did, and what I got: > > >>> vehicle='car' > >>> index = 0 > >>> lenght =len(vehicle) > >>> last = vehicle[lenght -1] > >>> while last >= vehicle[0]: > ... letter = vehicle[index] > ... print letter > ... last -= 1 > ... > c > Traceback (most recent call last): > File "", line 4, in ? > TypeError: unsupported operand type(s) for -=: 'str' > and 'int' > > As you can see, I am still a newbie... > Could anyone, please, guide me on this exercise? > Thanks! > Hoffmann A technique I used to find useful when I was very first learning (and struggling) was to calculate the variables for each pass of the loop (basically remove all the variable names, just like doing algebra). So: >>> vehicle='car' >>> index = 0 >>> lenght = len(vehicle) # therefore: >>> lenght = 3 >>> last = vehicle[lenght -1] # therefore: >>> last = vehicle[2] # therefore: >>> last = "r" >>> while "r" >= "c": # first pass ... letter = vehicle[index] # therefore: ... letter = vehicle[0] # therefore: ... letter = "c" ... print letter ... last -= 1 # therefore: ... "r" -= 1 # therefore: ... "r" = "r" - 1 # therefore: ... ERROR You'll find that that can make it much clearer what is actually happening. An alternative is to use lots and lots of print statements: >>> vehicle='car' >>> print vehicle >>> index = 0 >>> print index >>> lenght = len(vehicle) >>> print lenght and so on... It would be really good if there was a way to have a "verbose" interpreter that showed you each of the steps in the process, but I don't know of any. For example: >>> last = vehicle[lenght -1] last = vehicle[2] last = "r" >>> Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program for outputing the letter backward
--- Kent Johnson <[EMAIL PROTECTED]> 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
Re: [Tutor] Program for outputing the letter backward - almost there!
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
Re: [Tutor] Program for outputing the letter backward - almost there!
> 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 "", 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
Re: [Tutor] Program for outputing the letter backward - almost there!
--- Adam <[EMAIL PROTECTED]> 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
Re: [Tutor] Program for outputing the letter backward - almost there!
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
Re: [Tutor] Program for outputing the letter backward - almost there!
--- John Fouhy <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> 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 "", 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
Re: [Tutor] Program for outputing the letter backward
On 29/03/06, Hoffmann <[EMAIL PROTECTED]> 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
Re: [Tutor] Program for outputing the letter backward
--- John Fouhy <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > >>> vehicle='car' > > >>> index = 0 > > >>> lenght =len(vehicle) > > >>> last = vehicle[lenght -1] > > >>> while last >= vehicle[0]: > > ... letter = vehicle[index] > > ... print letter > > ... last -= 1 > > ... > > What is vehicle[index] ? > > What if I change index, say, > >index = index + 1 > > Now what is vehicle[index] ? > > -- > John. > ___ Hi John, (1) vehicle[index] is: 'c' (2) If index = index = 1, so vehicle[index] becomes: 'a' I changed a bit more the code, but I am still in trouble. please, take a look: (1st. try): >>> vehicle='car' >>> index = vehicle[-1]#'r' >>> lenght =len(vehicle) # 3 leters >>> last = vehicle[lenght -1] # >>> while last >= vehicle[0]: ... letter = vehicle[index] ... print letter ... last -= 1 ... Traceback (most recent call last): File "", line 2, in ? TypeError: string indices must be integers (2nd, try): >>> vehicle='car' >>> index = vehicle[-1] >>> while vehicle[-1] >= vehicle[0]: ... letter = vehicle[index] ... print letter ... index -= 1 ... Traceback (most recent call last): File "", line 2, in ? TypeError: string indices must be integers Such a 'hard' simple problem... 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
Re: [Tutor] Program for outputing the letter backward
On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > >>> vehicle='car' > >>> index = 0 > >>> lenght =len(vehicle) > >>> last = vehicle[lenght -1] > >>> while last >= vehicle[0]: > ... letter = vehicle[index] > ... print letter > ... last -= 1 > ... What is vehicle[index] ? What if I change index, say, index = index + 1 Now what is vehicle[index] ? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program for outputing the letter backward
--- John Fouhy <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > vehicle='car' > > index = vehicle[-1] #the last letter > > index_zero = vehicle[0] #the first letter > > > > while index >= index_zero: > >letter=vehicle[index] > >print letter > >index -= 1 > > > > The problem is that I get no output here. Could I > hear > > from you? > > I can print the letters backwards like this: > > vehicle = 'car' > print vehicle[2] > print vehicle[1] > print vehicle[0] > > Output: > > r > a > c > > - > > This is not very useful, though, because it will > only work for strings > that are exactly three letters long. Can you see > how to write a loop > to produe this output? > > Hint: the len() function will tell you how long a > string is. > > eg: if vehicle == 'car' then len(vehicle) == 3. > > -- > John. > ___ Hi John, I am still 'blind' here. Please, see below what I did, and what I got: >>> vehicle='car' >>> index = 0 >>> lenght =len(vehicle) >>> last = vehicle[lenght -1] >>> while last >= vehicle[0]: ... letter = vehicle[index] ... print letter ... last -= 1 ... c Traceback (most recent call last): File "", line 4, in ? TypeError: unsupported operand type(s) for -=: 'str' and 'int' As you can see, I am still a newbie... Could anyone, please, guide me on this exercise? 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
Re: [Tutor] Program for outputing the letter backward
Hoffmann wrote: > --- Kent Johnson <[EMAIL PROTECTED]> wrote: > >>You are confusing the index of a letter - the number >>which represents >>its position in the word - with the letter itself. >>In your code, index >>and index_zero are actually letters, not indices. >>Try to rewrite the >>code so they are numbers. > > Sorry for not showing the traceback the first time. > Please, see it below: > > Traceback (most recent call last): > File "", line 2, in ? > TypeError: string indices must be integers > > Any hint? Already gave the hint in the first part of my reply. The error message repeats it. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program for outputing the letter backward
--- Kent Johnson <[EMAIL PROTECTED]> wrote: > Hoffmann wrote: > > Hello: > > > > I am trying to write a code (this is an exercose > from > > a book). The goal is to write a program that takes > a > > string and outputs the letters backward, ine per > > line. > > Ok. I did a test first, by writing a code with > > numbers: > > > > a=0; b=10 > > while a<=b: > >print b > >b -= 1 > > > > Here the output is: > > 10 > > 9 > > 8 > > 7 > > 6 > > 5 > > 4 > > 3 > > 2 > > 1 > > 0 > > That worked fine. > > Now, back to my exercise. I tried to write a code > that > > takes the string 'car' as the input: > > > > vehicle='car' > > index = vehicle[-1] #the last letter > > index_zero = vehicle[0] #the first letter > > > > while index >= index_zero: > >letter=vehicle[index] > >print letter > >index -= 1 > > You are confusing the index of a letter - the number > which represents > its position in the word - with the letter itself. > In your code, index > and index_zero are actually letters, not indices. > Try to rewrite the > code so they are numbers. > > > > The problem is that I get no output here. > > My guess is you got a TypeError on the line >letter=vehicle[index] > > decause index is a letter. It's important to give us > accurate > descriptions of what happens, and to show error > messages and the > tracebacks that come with them. This can be very > helpful when you have a > problem. > > Kent > > ___ Hi Kent, Sorry for not showing the traceback the first time. Please, see it below: Traceback (most recent call last): File "", line 2, in ? TypeError: string indices must be integers Any hint? 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
Re: [Tutor] Program for outputing the letter backward
Hoffmann wrote: > Hello: > > I am trying to write a code (this is an exercose from > a book). The goal is to write a program that takes a > string and outputs the letters backward, ine per > line. > Ok. I did a test first, by writing a code with > numbers: > > a=0; b=10 > while a<=b: >print b >b -= 1 > > Here the output is: > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2 > 1 > 0 > That worked fine. > Now, back to my exercise. I tried to write a code that > takes the string 'car' as the input: > > vehicle='car' > index = vehicle[-1] #the last letter > index_zero = vehicle[0] #the first letter > > while index >= index_zero: >letter=vehicle[index] >print letter >index -= 1 You are confusing the index of a letter - the number which represents its position in the word - with the letter itself. In your code, index and index_zero are actually letters, not indices. Try to rewrite the code so they are numbers. > > The problem is that I get no output here. My guess is you got a TypeError on the line letter=vehicle[index] decause index is a letter. It's important to give us accurate descriptions of what happens, and to show error messages and the tracebacks that come with them. This can be very helpful when you have a problem. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program for outputing the letter backward
--- John Fouhy <[EMAIL PROTECTED]> wrote: > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > > vehicle='car' > > index = vehicle[-1] #the last letter > > index_zero = vehicle[0] #the first letter > > > > while index >= index_zero: > >letter=vehicle[index] > >print letter > >index -= 1 > > > > The problem is that I get no output here. Could I > hear > > from you? > > I can print the letters backwards like this: > > vehicle = 'car' > print vehicle[2] > print vehicle[1] > print vehicle[0] > > Output: > > r > a > c > > - > > This is not very useful, though, because it will > only work for strings > that are exactly three letters long. Can you see > how to write a loop > to produe this output? > > Hint: the len() function will tell you how long a > string is. > > eg: if vehicle == 'car' then len(vehicle) == 3. > > -- > John. > ___ Hi John and the other colleagues from the Tutor, I still didn't realized how to solve this exercise. Regarding the for loop. I can do that for the "forward" version of the program. See below: name = 'car' for char in name: print char However, I still write a "backward" version (in order to get r a c Could you guys, please, continue talking to me? 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
Re: [Tutor] Program for outputing the letter backward
Hoffmann wrote: > while index >= index_zero: >letter=vehicle[index] >print letter >index -= 1 > > The problem is that I get no output here. Could I hear > from you? Hi, remember that the condition for the while has to be true. When does index >= index_zero stop being true??? Hope that gets you going, Hugo Ps: there are easier ways of doing this in Python, consider the "for" statement. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program for outputing the letter backward
On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote: > vehicle='car' > index = vehicle[-1] #the last letter > index_zero = vehicle[0] #the first letter > > while index >= index_zero: >letter=vehicle[index] >print letter >index -= 1 > > The problem is that I get no output here. Could I hear > from you? I can print the letters backwards like this: vehicle = 'car' print vehicle[2] print vehicle[1] print vehicle[0] Output: r a c - This is not very useful, though, because it will only work for strings that are exactly three letters long. Can you see how to write a loop to produe this output? Hint: the len() function will tell you how long a string is. eg: if vehicle == 'car' then len(vehicle) == 3. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor