Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that your <code>my_lst[y]</code> is a string so it cannot be compared to an <code>int</code>. Here's some code that will produce the output that you want. I've also included some things that will make your python code look better!</p> <pre><code>my_lst = [str(x) for x in range(10)] weird_num = "" for counter, y in enumerate(range(10), 1): if int(my_lst[y]) &lt;= 5: weird_num = weird_num + my_lst[y] print weird_num, counter </code></pre> <p>This will produce the output that you want.</p> <p>Now, let me explain some of the parts I included. On line <code>1</code>, instead of creating an empty list, and then populating it with a for loop, I am using a list comprehension to populate the list the minute I make it. More info about list comprehensions can be found <a href="http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">here</a>. Basically, it's sticking a a for loop onto one list inside a list that populates the list.</p> <p>Next, I made <code>weird_num</code> an empty list. This is so that you can start concatenating your <code>my_lst</code> elements inside the for loop. If you were to initialize it with <code>weird_num = "0"</code>, you would have an extra zero on the left side of your output.</p> <p>For the for loop, I used an <code>enumerate</code>, more info for which can be found <a href="http://docs.python.org/2/library/functions.html#enumerate" rel="nofollow">here</a>. It turns your for loop into an enumerate object, which means you can keep track of a variable like <code>counter</code> through the whole thing. <code>counter</code> will go from 0 -> 9, but I added the extra parameter of <code>1</code> so that it will go from 1 -> 10.</p> <p>Finally, inside the for loop, I just had to cast <code>my_lst[y]</code> to <code>int</code> so that you can do the comparison logic.</p> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload