By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

Re: A quick question

P: n/a
On Wed, 28 May 2008 10:25:01 -0000
"James" <co******@msn.comwrote:
Hey everyone,

I just started using python and cant figure this out, I'm trying to
make a program where someone types in a word and the program gives it
back backwards. For example if the person puts in "cat" I want the
program to give it back as "tac" and what it does is prints out 3,2,1.
How can I get these integers to print as letters? This is what I have,

word = raw_input("Type a word:")
start = len(word)

for letter in range(start, 0, -1):
print letter
Ignore my previous message. Too early. Here is your script:

word = raw_input("Type a word:")
for i in range(len(word), 0, -1):
print word[i]

Sorry about that.

--
D'Arcy J.M. Cain <da***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
D'Arcy J.M. Cain a écrit :
On Wed, 28 May 2008 10:25:01 -0000
"James" <co******@msn.comwrote:
>Hey everyone,

I just started using python and cant figure this out, I'm trying to
make a program where someone types in a word and the program gives it
back backwards. For example if the person puts in "cat" I want the
program to give it back as "tac" and what it does is prints out 3,2,1.
How can I get these integers to print as letters? This is what I have,

word = raw_input("Type a word:")
start = len(word)

for letter in range(start, 0, -1):
print letter

Ignore my previous message. Too early. Here is your script:

word = raw_input("Type a word:")
for i in range(len(word), 0, -1):
print word[i]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range

sequence indices are zero-based, so you want to start at len(word) -1.
And the range upper limit (second argument) is not included. So you want
to pass -1 here.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.