why ? ( list index out of range )

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1934 Views - Last Post: 13 December 2012 - 10:20 AM Rate Topic: -----

#16 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: why ? ( list index out of range )

Posted 13 December 2012 - 06:37 AM

View Postjon.kiparsky, on 12 December 2012 - 12:55 PM, said:

No sweat. Best of luck!
As you get the hang of this, you might find that it works better to do this in a list comprehension or a filter - those seem more "pythonic" approaches to this sort of problem - but for now, just getting away from the iteration by index will help a lot.



I tookunder consideration all you said about indexes ^^ so what i made is this one, i think it works but i would love to listen to what you think about it too ^^ thanks for all your help

import random



lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]
x=random.sample(lists,  5)
theitems=[]
for i in range(len(x)):
        theitems.append(x[i][2])
print theitems
tw=0
tv=0
while True:
    

    epilogi=raw_input("choose")
    z=0
    for i in theitems:
            
     
              if i==epilogi:
              
                      
                      
                  tw=tw+x[z][1]
                  tv=tv+x[z][0]
                  theitems.remove(i)
                  x.remove(x[z])
              z=z+1
    print theitems,x
   


Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: why ? ( list index out of range )

Posted 13 December 2012 - 10:13 AM

Some things improved, some things I'm wondering about.

What is it you're trying to achieve here, anyway?
Was This Post Helpful? 0
  • +
  • -

#18 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: why ? ( list index out of range )

Posted 13 December 2012 - 10:16 AM

View Postjon.kiparsky, on 13 December 2012 - 10:13 AM, said:

Some things improved, some things I'm wondering about.

What is it you're trying to achieve here, anyway?

i am trying to remove from the list these two lists (x and theitems) the index with the word that the user types :)
Btw, if you have time check my other post about timers in python cause its something i have no clue about

Thannks :)
Was This Post Helpful? 0
  • +
  • -

#19 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9048
  • View blog
  • Posts: 33,970
  • Joined: 12-June 08

Re: why ? ( list index out of range )

Posted 13 December 2012 - 10:17 AM

lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]

With a list like that - whew.. either that's a really fun weekend or a nightmare morning.
Was This Post Helpful? 1
  • +
  • -

#20 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: why ? ( list index out of range )

Posted 13 December 2012 - 10:20 AM

View Postmodi123_1, on 13 December 2012 - 10:17 AM, said:

lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]

With a list like that - whew.. either that's a really fun weekend or a nightmare morning.



it is a part of a knapsack solver the first is the value the second the weight and the last the name of the 'item'

and yeah the item names are random/dump :P

This post has been edited by fledgeling: 13 December 2012 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2