Test a list

A

Ana Dionísio

t= [3,5,6,7,10,14,17,21]

Basically I want to print Test 1 when i is equal to an element of the list "t" and print Test 2 when i is not equal:


while i<=25:

if i==t[]:

print "Test1"

else:

print "Test2"

What is missing here for this script work?

Thank you all
 
T

timothy crosley

Hi Ana,

if I understand your question correctly, all you have to do to test this isto write:

if i in t:
print "Test1"
else:
print "Test2"
 
J

Joel Goldstick

t= [3,5,6,7,10,14,17,21]

Basically I want to print Test 1 when i is equal to an element of the list
"t" and print Test 2 when i is not equal:


while i<=25:

You test i, but you don't set i to anything, or change it in your loop

you may want to try

for i in range(25) (or perhaps range(1,26) i'm guessing. Be more clear
if i==t[]:

This isn't legal syntax. Try googling about for loops and sets. Try again
and come back
 
T

Tim Chase

t= [3,5,6,7,10,14,17,21]

Basically I want to print Test 1 when i is equal to an element of
the list "t" and print Test 2 when i is not equal:

while i<=25:
if i==t[]:
print "Test1"
else:
print "Test2"

What is missing here for this script work?

Well, your code never increments "i", so it will loop forever; you
also don't subscript "t" with anything, so you have invalid syntax
there; you also don't have any values in the list that actually match
their 0-indexed offset, so even if your code was correct, it would
still (correctly) return Test2 for everything.

This sounds a bit like homework, but the Pythonic way would likely
iterate over the data and its enumeration:

for index, value in enumerate(t):
if index == value: # compare index & value accordingly
...

If you need to have the index start at 1 (or some other value)
instead of 0, and you're running Python2.6+, you can pass the initial
index to enumerate(). Otherwise, you have to do the math in your
comparison.

-tkc
 
S

Steven D'Aprano

t= [3,5,6,7,10,14,17,21]

Basically I want to print Test 1 when i is equal to an element of the
list "t" and print Test 2 when i is not equal:

Wouldn't it make more sense to print "Equal" and "Not equal"?

If all you want to do is check whether some value i can be found in the
list, you can do this:

if i in t:
print "Found"
else:
print "Not found"



If you want to find the index of where a value is found, use the index
method:

x = 10
t.index(x) # returns 4


while i<=25:
if i==t[]:
print "Test1"
else:
print "Test2"

What is missing here for this script work?

Lots of things. What's i? Does it ever change, or is it a constant? What
are you comparing it to?


Taking a wild guess at what you mean, you could try this:


for i in range(25):
print i, "found" if i in t else "not found"



Does this help?
 
J

John Gordon

t= [3,5,6,7,10,14,17,21]
Basically I want to print Test 1 when i is equal to an element of the
list "t" and print Test 2 when i is not equal:
while i<=25:
if i==t[]:
print "Test1"

print "Test2"
What is missing here for this script work?

1. You're missing an initial value for i.
2. You never increment i, so the while loop will never exit.
3. The syntax 'if i==t[]' is wrong.
4. The way you have this code set up, you would need two loops: an outer
loop for i from 1 to 25, and an inner loop for the items in t.
5. As others have said, this is a poor way to go about it. You want to
use "if i in t".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top