如何创建循环的随机字母生成器

发布于 2025-01-16 16:13:59 字数 609 浏览 4 评论 0原文

所以我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-01-23 16:13:59

您发布的示例(aaa、aab、abb 等)对我来说似乎不像随机字母生成器,但这是我用来随机生成 3 个字母字符串的方法:

# choice allows us to randomly choose an element from a list
from random import choice

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

randomString = ''

for i in range(3):
    randomString += choice(letters)

如果您想创建这些字符串的列表,然后只需循环最后 3 行几次并将结果添加到如下列表中:

charList = []

for i in range(x):
    randomString = ''

    for i in range(3):
        randomString += choice(letters)
    
    charList.append(randomString)

其中 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:

# choice allows us to randomly choose an element from a list
from random import choice

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

randomString = ''

for i in range(3):
    randomString += choice(letters)

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:

charList = []

for i in range(x):
    randomString = ''

    for i in range(3):
        randomString += choice(letters)
    
    charList.append(randomString)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文