擅长:python、mysql、java
<p>如果你搬家</p>
<pre><code>alphabet = removeChar(alphabet, ch)
</code></pre>
<p>在for循环之外,它将工作。您得到的索引超出范围,因为removeChar每次迭代都将索引减少1。你知道吗</p>
<pre><code>import random
def removeChar(string, idx):
return string[:idx] + string[idx+1]
def keyGen():
alphabet = 'abcdefghijklmnopqrstuvwxyz '
key = ""
for i in range(len(alphabet)-1):
ch = random.randint(0,25-i)
key = key + alphabet[ch]
removeChar(alphabet, ch)
return key
print(ch, alphabet)
keyGen()
</code></pre>
<p>这会给你一个类似这样的输出:</p>
<pre><code>"xtcrmofnojnenkidfaaabdccba"
</code></pre>
<p>如果这不是您想要的输出,请给我们一个您想要的输出的例子。你知道吗</p>