|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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]
#testing your function
a=str(raw_input("enter a word."))
linedown(a)
you really need to use code tags when posting python code...
__________________
Jack --------- use code tags become vegetarian python? yes, sir! unarm.org get firefox If I helped you then please click the " " in the upper right-hand corner of my post.
|
|
#6
|
||||
|
||||
|
Quote:
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 ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#8
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
__________________
Miscellaneous Software Viper_SB Developershed E-Support Anyone else play chess? Challenge me |
|
#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 |