Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41613
| Date | 2013-03-20 13:36 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: Test a list |
| References | <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3573.1363804459.2939.python-list@python.org> (permalink) |
On 2013-03-20 11:15, Ana Dionísio wrote:
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Test a list Ana Dionísio <anadionisio257@gmail.com> - 2013-03-20 11:15 -0700 Re: Test a list timothy crosley <timothy.crosley@gmail.com> - 2013-03-20 11:24 -0700 Re: Test a list Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-20 14:26 -0400 Re: Test a list Tim Chase <python.list@tim.thechases.com> - 2013-03-20 13:36 -0500 Re: Test a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-20 18:40 +0000 Re: Test a list John Gordon <gordon@panix.com> - 2013-03-20 20:39 +0000
csiph-web