<p>不要将函数调用用作默认值,因为这意味着您在定义函数时调用的是input(),而不是在调用函数时。这个input()将被调用一次,而且只调用一次,并且这将在导入模块时发生,而不是在调用函数时发生。你知道吗</p>
<pre><code>def break_cipher(OriginalText):
</code></pre>
<p>不要用CamelCase命名变量,也不要用大写字母开头(这是类的惯例)</p>
<pre><code>def break_cipher(original_text)
</code></pre>
<p>使用dict代替这些变量:</p>
<pre><code>count_dict = {}
</code></pre>
<p>您的任务是遍历字符串,使用循环:</p>
<pre><code>for character in original_text:
if character not in count_dict:
count_dict[character] = 0
count_dict += 1
</code></pre>
<p>在循环结束时,您将看到以下内容:</p>
<pre><code>{
'a': 5,
'e': 7
}
</code></pre>
<p>等等。。。你知道吗</p>
<p>您还可以使用<a href="https://docs.python.org/2/library/collections.html#collections.defaultdict" rel="nofollow noreferrer">defaultdict</a>来进一步简化代码</p>
<pre><code>from collections import defaultdict
count_dict = defaultdict(int) # Note that new int's are 0
for character in original_text:
count_dict[character] += 1
</code></pre>
<p>可能有许多细微之处和改进:</p>
<p>A==A吗?在这种情况下,您需要:</p>
<pre><code>count_dict[character.lower()] += 1
</code></pre>
<p>你打算只数英文字母字符吗?在这种情况下,您需要:</p>
<pre><code>for character in [charr for charr in count_dict if 'a' < charr.lower() < 'z']
</code></pre>
<p>(这个见<a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow noreferrer">list comprehensions</a>)</p>
<p>对于替换零件:</p>
<pre><code>frequencies = count_dict.keys() # This gives you a list of all the letters in the text
frequencies.sort(key=count_dict).reverse() # This will sort them by frequency
</code></pre>
<p>然后你只需要重新创建文本</p>
<p>愿密码与你同在</p>