GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Dec-2008, 03:11
takachi takachi is offline
Awaiting Email Confirmation
 
Join Date: Dec 2007
Posts: 101
takachi is an unknown quantity at this point

Loops and strings


hello.I'm quite new to the python language and I'm trying to write a simple program which gets a sentence and print the number of digits,vowels,spaces and other chars.I've written a program but it doesn't work.
can you tell me where is the problem and maybe dirrect me to a good tutorial.
this is my code so far:
Python Code:
vowels=other=spaces=digits=0
sentence=raw_input()
for i in len(sentence):
      if sentence[i]=='o':vowels=vowels+1
      elif sentence[i]=='O':vowels=vowels+1
      elif sentence[i]=='a':vowels=vowels+1
      elif sentence[i]=='A':vowels=vowels+1
      elif sentence[i]=='u':vowels=vowels+1
      elif sentence[i]=='U':vowels=vowels+1
      elif sentence[i]=='i':vowels=vowels+1
      elif sentence[i]=='I':vowels=vowels+1
      elif sentence[i]=='e':vowels=vowels+1
      elif sentence[i]=='E':vowels=vowels+1
      elif sentence[i]==' ':spaces=spaces+1
      elif '9'>=sentence[i]>='0':digits=digits+1      
      else:other=other+1
print "vowels= ",vowels
print "spaces: ",spaces
print "digits: ",digits
print "other: ",other       
also how can I write an if statement with more than one condition.
Last edited by LuciWiz : 25-Dec-2008 at 19:33. Reason: Replaced [cpp] tags with [py] tags
  #2  
Old 25-Dec-2008, 11:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: loops and strings


Quote:
Originally Posted by takachi
hello.I'm quite new to the python language

If you don't have a textbook (or, maybe, even if you do), you might start here: http://docs.python.org/tutorial/


Quote:
Originally Posted by takachi
...how can I write an if statement with more than one condition.

So far, you haven't written an if statement with even one condition.

From the tutorial: "An informal Introduction...->First Steps Towards Programming

"The body of the loop is indented: indentation is Python’s way of grouping statements. "

So:
Python Code:
.
.
.
sentence=raw_input()
for i in range(0,len(sentence)):
    if sentence[i]=='o':
        vowels=vowels+1
    elif sentence[i]=='O':
        vowels=vowels+1
.
.
.

Note the proper form of the "if" statement giving a range as i takes on values from the the value of the first argument up to but not including the value of the second argument.

Regards,

Dave
Last edited by davekw7x : 25-Dec-2008 at 12:32.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats wrong? Using a function in strings. 8100 Power C++ Forum 2 16-Oct-2008 16:46
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 15:43
Storing strings in an array... MSUracer C++ Forum 27 21-Oct-2006 02:24
Menu of character strings aijazbaig1 C Programming Language 21 27-Jul-2006 04:07
Dynamic memory, loops, and segmentation faults ubergeek C++ Forum 2 07-May-2005 17:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.