Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2007-09-09 06:58:11
Size: 357
Editor: c-69-252-155-205
Comment:
Revision 11 as of 2011-03-26 23:30:26
Size: 996
Editor: PaulBoddie
Comment: Added categories, title.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= How to store information in variable arrays? =
Line 3: Line 5:
while x != 10:
     text[x] = int(raw_input("Enter a number: "))
{{{#!python
x = 1
text = []
while x <= 10:
     text.append(int(raw_input("Enter a number: ")))
Line 7: Line 12:
for x in range 10: for x in range(10):
Line 9: Line 14:
}}}
Line 10: Line 16:
I just need to do this to compare a history to make sure a number hasn't already been entered earlier. But this is just a small example I just need to do this to compare a history to make sure a number hasn't already been entered earlier. But this is just a small example. I get nasty errors.

Thanks!

edit: that should work now
We initialize a list first with `text = []`.
Then we just append each element to that list.

----

You can use the `in` operator:

{{{#!python
if number in text:
    print "Number already entered!"
}}}

If you choose to use a set instead of a list...

{{{#!python
text = set()
}}}

...then the above test still works, but you'll use the `add` method on the set when adding numbers, not an `append` method.
----
CategoryAskingForHelp CategoryAskingForHelpAnswered

How to store information in variable arrays?

How would I store information in variables that I can go back later and compare? Such as

   1 x = 1
   2 text = []
   3 while x <= 10:
   4      text.append(int(raw_input("Enter a number: ")))
   5      x = x + 1
   6 
   7 for x in range(10): 
   8     print text[x]

I just need to do this to compare a history to make sure a number hasn't already been entered earlier. But this is just a small example. I get nasty errors.

Thanks!

edit: that should work now We initialize a list first with text = []. Then we just append each element to that list.


You can use the in operator:

   1 if number in text:
   2     print "Number already entered!"

If you choose to use a set instead of a list...

   1 text = set()

...then the above test still works, but you'll use the add method on the set when adding numbers, not an append method.


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/How to store information in variable arrays? (last edited 2011-03-26 23:30:26 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.