[Tutor] trouble with "if"

Alan Gauld alan.gauld at btinternet.com
Fri May 25 00:34:05 CEST 2007


"adam urbas" <adamurbas at hotmail.com> wrote 

>  It won't even accept words.  
> I can only get it to accept numbers.  

try this(untested code!):

number = None
data = raw_input('Type something: ')
try: number = int(data)
except: data = data.split()    # assume a string

if number:    # user entered a number
    if number == 1:  print 'circle'
    elif number == 2: print 'another'
else:         # user entered words
    if data[0].lower() == 'circle': print 'circle'
    else: print 'user entered ', data[0]

Notice that to use ithe input as a number you have to 
convert the raw input characters to a number (using int)
To get the individual words we can use split() which by 
default splits a string into the individual words.

Is that the kind of thing you mean?

I've no idea what a Ti83 is BTW. :-)

Alan G.



More information about the Tutor mailing list