Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is awkwardly phrased, but I think by <em>'read an unspecified number'</em> what you are looking for is generating a random value - in which case, you want <code>random.randint()</code>.</p> <pre><code>&gt;&gt;&gt; import random &gt;&gt;&gt; random.randint(1, 100) 17 &gt;&gt;&gt; random.randint(1, 100) 30 &gt;&gt;&gt; random.randint(1, 100) 84 </code></pre> <p>Also note that you don't need to do so many loops to achieve what you are trying to do. If you are looping over a range of indices in python rather than looping over the list itself, you are doing something wrong. In this case, it's far easier to use <a href="http://docs.python.org/3/library/functions.html#sum" rel="nofollow">the built in <code>sum()</code> function</a>.</p> <pre><code>&gt;&gt;&gt; import random &gt;&gt;&gt; numbers = [random.randint(0, 100) for _ in range(100)] &gt;&gt;&gt; numbers [45, 81, 46, 80, 67, 57, 72, 47, 87, 13, 5, 56, 87, 5, 74, 7, 88, 1, 36, 48, 97, 39, 16, 31, 52, 30, 14, 58, 43, 17, 70, 85, 37, 49, 28, 1, 33, 73, 90, 86, 75, 42, 27, 91, 100, 86, 59, 85, 43, 59, 27, 24, 11, 56, 95, 36, 3, 10, 40, 42, 10, 71, 75, 10, 69, 86, 63, 6, 56, 95, 91, 31, 40, 13, 5, 5, 31, 28, 36, 94, 22, 77, 100, 36, 98, 85, 23, 61, 92, 94, 17, 91, 25, 43, 55, 7, 9, 57, 14, 39] &gt;&gt;&gt; average = sum(numbers)/len(numbers) &gt;&gt;&gt; average 49 &gt;&gt;&gt; sum(number &gt; average for number in numbers) 46 &gt;&gt;&gt; sum(number &lt; average for number in numbers) 53 </code></pre> <p>Here I have also used <a href="http://www.youtube.com/watch?v=t85uBptTDYY" rel="nofollow">list comprehensions and generator expressions</a>, which are useful for these kind of operations.</p> <p>Alongside this, <code>eval()</code> is a really bad way to get a number from a string. Use <code>int(some_string)</code> instead.</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. 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