[Tutor] List help
Emile van Sebille
emile at fenx.com
Sun Nov 21 00:37:35 CET 2010
On 11/20/2010 11:06 AM george wu said...
> x=0
> y=0
> w=raw_input("Input: ")
> w=list(w)
> for x in range(len(w)):
> a=w[x]
> t=0
> print a
> if a==2 or a==4 or a==6 or a==8 or a==10:
> t=a/2
> print "hi"
>
> When I run this program, it doesn't print "hi". Can you please tell me why?
>
When you're comparing a to 2,4,6,8,10 a is a string representing the
nth position of the input value w as iterated over with x.
Specifically, a=w[x] which is the list of the input value.
>>> list("hello")
['h', 'e', 'l', 'l', 'o']
>>> list("1234")
['1', '2', '3', '4']
You then compare a string to the numbers 2,4,6,8,10
making the test line:
if a=="2" or a=="4 or a=="6" or a=="8":
should make it print hi. Note I dropped the 10 as a single charter
string would never match.
All this is likely beside the point -- what were you trying to have happen?
Emile
More information about the Tutor
mailing list