### Thread: need some help on this problem

Page 1 of 2 12 Last
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2004
Posts
13
Rep Power
0

#### need some help on this problem

I'm suppose to get any word from a user and print out the letters of the words downwards like so :

F
u
n
c
t
i
o
n

and i'm suppose to make it a function in order to do this
i thought that

def line(str):
""" takes in a string and print out it one by one."""
a=str(raw_input("line: "))
return a

would be the function, but i don't know what to do next. plz help me out.
2. think about it - if we're given a string with length N, we're going to need to print on N letters, right? now, take a look at the for loop, the len() function for strings, the range() function, and slice notation for strings.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2004
Posts
13
Rep Power
0
i have this so far

def line(str):
""" takes in a string and print out it one by one."""
a=str(raw_input("line: "))
return a
for i in range(0,len(a)):
print i

it doesn't even prompt for user to input, and i'm not too sure about my for loop
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2004
Posts
13
Rep Power
0

now i have this

a=str(raw_input("enter a word."))
s=str(a)
for i in range(0,len(s)):
print i

and it prints out the number of letter in the word in a column
eg, i input good

0
1
2
3

how do i change the numbers to the letters of the words
5. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
Lawrence, Kansas [KU]
Posts
1,559
Rep Power
15
considering you actually seem to be trying, this should do what you want.

Code:
```#your function
def linedown(str):
for i in range(0,len(str)):
print str[i:i+1]

a=str(raw_input("enter a word."))
linedown(a)```
you really need to use code tags when posting python code...
6. Originally Posted by cam_k
now i have this

a=str(raw_input("enter a word."))
s=str(a)
for i in range(0,len(s)):
print i

and it prints out the number of letter in the word in a column
eg, i input good

0
1
2
3

how do i change the numbers to the letters of the words
jacktasia's example should work - remember what I said about slice notation? that's how you convert string indices to the characters themselves.

Code:
`print str[i:i+1]`
is slice notation for printing the character(s) between indice i and indice i+1 - essentially the (i+1)th character in the string. since range(len(s)) gives us a range of numbers from zero to one less than the number of characters in the string, the slice notation will give us the 1st through the last characters of the string when we loop through it.
7. Slightly more concise is :
Code:
```>> >  def printChr(string):
for char in string:
print char```
grim
8. Originally Posted by deltacoder
jacktasia's example should work - remember what I said about slice notation? that's how you convert string indices to the characters themselves.

Code:
`print str[i:i+1]`
is slice notation for printing the character(s) between indice i and indice i+1 - essentially the (i+1)th character in the string. since range(len(s)) gives us a range of numbers from zero to one less than the number of characters in the string, the slice notation will give us the 1st through the last characters of the string when we loop through it.
Actually the indice [i:i + 1] returns the same value as [i], for example:

Code:
```>>> string = 'string'
>>> for number in range(len(string)): print string[number: number + 1]

s
t
r
i
n
g
>>> for number in range(len(string)): print string[number]

s
t
r
i
n
g
>>>```
You should also remember that the raw_input() function returns a sting bu default, so there is no need to try and type-cast it with str(). Also note, that if you don't give range() a starting number explicitly then 0 is used.

Over all though, I would have gone down the same rout as Grim in his example .

Anyway have fun guys ,

Mark.
Last edited by netytan; October 19th, 2004 at 07:29 AM. Reason: Colored Example
9. Originally Posted by netytan
Actually the indice [i:i + 1] returns the same value as [i]
Yeah, but as it was apparent cam_k didn't know much about either method, it made sense to help him understand the more versatile one so that he could apply it to other problems as well. It's all good in the end though.
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2004
Posts
13
Rep Power
0
thanks a lot u us. i'm really new to python, so i don't really know much of it. I'm really glad to have this forum to help me out on questions that i don't understand
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2004
Posts
13
Rep Power
0
i got another question
how do u make the words go like a piramid.
eg
>>>enter word: good

g
go
goo
good
12. a for loop should work for it, just save each letter you come across and print it out also in the next loop
13. this one just about screams slice notation! take a look at the following example:

Code:
```str = raw_input("Enter a string:")
for x in range(len(str)):
print str[:x+1]```
14. Another example. This one shifts the addition into range() rather than adding it to the indice each time.

Code:
```>>> def printPyramid(string):
for indice in xrange(len(string) + 1):
print string[:indice]

>>> prinPyramid(raw_input('Eneter a string to print: '))
Eneter a string to print:

>>> prinPyramid(raw_input('Eneter a string to print: '))
Eneter a string to print: Hello printPyramid

H
He
Hel
Hell
Hello
Hello
Hello p
Hello pr
Hello pri
Hello prin
Hello print
Hello printP
Hello printPy
Hello printPyr
Hello printPyra
Hello printPyram
Hello printPyrami
Hello printPyramid
>>>```
It's also not the best idea in the world to over ride Pythons build-in functions - spacifically file(), str(), int() etc. - in your own programs.

That said, if you can garentee that the function won't be used anywhere in the program then go for it . But it's generally seen as a bad practice.

Have fun,

Mark.
15. Just thought I'd another small example here. Its the same idea only the pyramid is printed upside down. which should technically fall over . Either way, it's just for fun so we can ignore the physics of this.

Code:
```>>> string = 'string'
>>> for slice in range(len(string)):
print string[slice:]

string
tring
ring
ing
ng
g
>>>```
Enjoy,

Mark.
Page 1 of 2 12 Last