Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on the error you posted, I think this should help.</p> <p>In Python, you can have local and global variables of the same name. The <code>freq</code> in the function is local, and thus the initialization of the global <code>freq</code> doesn't touch the local one. To use the global <code>freq</code>, you have to tell the function to do so, via the <a href="http://docs.python.org/release/2.4/ref/global.html" rel="nofollow">global statement</a>. This is explained a little more in the <a href="http://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python" rel="nofollow">Python FAQs</a>.</p> <p>That should be enough to get you back on track.</p> <p><strong>EDIT:</strong> Below is an edit of your <code>decrypt</code> function, with some explanations of the changes</p> <pre><code>def decrypt(): # we don't need the zero start value, that's the default # test all possible shifts for i in range(27): # initialize the array freqlist = [] # shift all the letters in the input for a in inpt: b = ord(a) b -= i if b &gt; ord("Z"): b -= 26 elif b &lt; ord("A"): b+=26 freqlist.append(b) # now put the shifted letters back together shifted = "" for a in freqlist: d = chr(a) # append the shifted letter onto our output shifted += d # after we put the decrypted string back together, print it # note this is outside the letter loops, # but still inside the possible shifts loop # thus printing all possible shifts for the given message print(d) </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