Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 18th, 2004, 11:35 PM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old October 18th, 2004, 11:45 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
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.
__________________

deltacoder.com :: W3C Standards :: PHP.net :: Google

Reply With Quote
  #3  
Old October 19th, 2004, 12:00 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #4  
Old October 19th, 2004, 01:03 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow

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

Reply With Quote
  #5  
Old October 19th, 2004, 02:07 AM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 4 m 35 sec
Reputation Power: 9
Send a message via AIM to jacktasia
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.

Reply With Quote
  #6  
Old October 19th, 2004, 03:28 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
Quote:
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.

Reply With Quote
  #7  
Old October 19th, 2004, 03:52 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Slightly more concise is :
Code:
>> >  def printChr(string): 
	for char in string: 
		print char


grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #8  
Old October 19th, 2004, 07:22 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : October 19th, 2004 at 07:29 AM. Reason: Colored Example

Reply With Quote
  #9  
Old October 19th, 2004, 02:35 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
Quote:
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.

Reply With Quote
  #10  
Old October 20th, 2004, 12:10 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #11  
Old October 20th, 2004, 12:16 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i got another question
how do u make the words go like a piramid.
eg
>>>enter word: good

g
go
goo
good

Reply With Quote
  #12  
Old October 20th, 2004, 12:59 AM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,795 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 23 h 47 m 58 sec
Reputation Power: 437
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

Reply With Quote
  #13  
Old October 20th, 2004, 02:58 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
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]

Reply With Quote
  #14  
Old October 20th, 2004, 06:06 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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