Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Append the values to a list:</p> <pre><code>L = [] for i in range(n): L.append(raw_input('Rating: ')) </code></pre> <p>Then, you can get the average of the list:</p> <pre><code>sum(map(int, L)) / len(l) </code></pre> <p>Or, you can use a list comprehension:</p> <pre><code>L = [int(raw_input('Rating: ')) for _ in range(n)] sum(L) / len(L) </code></pre> <hr> <p>However, if you're dealing with floats, then use this to get the average:</p> <pre><code>from __future__ import division sum(L) / len(L) </code></pre> <p><code>from __future__ import division</code> changes the <code>/</code> to a true division operator. True division is active in python 3, but as you are on python 2.7, you can import it directly.</p> <hr> <p>With your code, there are some problems:</p> <p><code>L.append(raw_input('Rating: '))</code> returns <code>None</code>. You don't need to assign it to a variable, so get rid of the <code>m =</code>.</p> <p>However, it seems you want to keep the value of <code>m</code> so you can write it in the file. You can then do something like:</p> <pre><code>m = raw_input("Rating: ") L.append(m) </code></pre> <p>There is also no need for <code>str(m)</code>, as <code>raw_input()</code> returns a string.</p> <hr> <p>And finally, you should use the <code>with</code> statement when opening files. This saves calling <code>close()</code>:</p> <pre><code>with open('music.txt','a') as f: for i in range(n): line = raw_input('Next Track: ') m = raw_input("Rating: ") L.append(m) overall = sum(map(int, L)) / len(L) f.write("Track Name: " + line + " - Rating: " + m + "\n") </code></pre>
 

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