如何创建循环的随机字母生成器
所以我对 python 很陌生,只是想创建一个字母生成器。 输出应如下所示:aaa aab abb bbb aac acc ccc ...
没有大写字母,没有数字,没有双输出,只有 3 个字母长的随机字母循环。 希望有人可以帮助我,问候
编辑:我现在已经创建了一个工作代码,可以生成一个 3 个字母长的单词,但现在我遇到了它们生成多次的问题。我知道循环函数看起来很奇怪,但我的意思是它有效。
import string, random
count = 0
while count < 1:
randomLetter1 = random.choice(
string.ascii_lowercase
)
randomLetter2 = random.choice(
string.ascii_lowercase
)
randomLetter3 = random.choice(
string.ascii_lowercase
)
print(randomLetter1 + randomLetter2 + randomLetter3)
So I´m very new to python and just trying to create a letter generator.
The output should be like this: aaa aab abb bbb aac acc ccc ...
No uppercase letters, no digits, no double outputs, just a 3 letter long random letter loop.
Hope Someone can help me, Greetings
Edit: I´ve now created a working code that generates a 3 letter long word but now I have the problem that they are getting generated several times. I know the loop function looks weird but I mean it works.
import string, random
count = 0
while count < 1:
randomLetter1 = random.choice(
string.ascii_lowercase
)
randomLetter2 = random.choice(
string.ascii_lowercase
)
randomLetter3 = random.choice(
string.ascii_lowercase
)
print(randomLetter1 + randomLetter2 + randomLetter3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的示例(aaa、aab、abb 等)对我来说似乎不像随机字母生成器,但这是我用来随机生成 3 个字母字符串的方法:
如果您想创建这些字符串的列表,然后只需循环最后 3 行几次并将结果添加到如下列表中:
其中 x 是要生成的字符串数。肯定还有很多其他方法可以完成类似的操作,但正如您所提到的,您是初学者,这可能是最简单的方法。
编辑:至于您发布的新问题,您只是忘记了增加 count 变量,这导致 while 循环无限运行。一般来说,如果您看到循环无限继续,您应该立即检查退出条件是否变为真。
The example you posted (aaa, aab, abb, etc.) does not seem like a random letter generator to me, but here's the method I would use to randomly generate 3-letter strings:
If you want to create a list of these strings, then just loop the last 3 lines several times and add the results to a list like this:
where x is the number of strings you want to generate. There are definitely many other ways to do something like this, but as you mentioned you're a beginner this would probably be the easiest method.
EDIT: As for the new problem you posted, you've simply forgotten to increment the count variable, which leads to the while loop running infinitely. In general, should you see a loop continue infinitely you should immediate check to see if the exit condition ever becomes true.