如何在 python 中重新滚动 randint() ? [已关闭]

问题描述 投票:0回答:1

当我插入一些元音和辅音时,我做了一个为我制作音节的东西:

import random
vowels = ["a", "a", "ä", "e", "e", "ë", "œ", "ø", "i", "i", "ï", "o", "o", "ö", "u", "u", "ü",
          "ă", "ĕ", "ô", "au", "ou", "a", "a", "ä", "e", "e", "ë", "œ", "ø", "i", "i", "ï", "o", "o", "ö", "u", "u", "ü"
          , "ă", "ĕ", "ô", "au", "ou", "a", "e", "i", "o", "u", "ar", "er", "ir", "or", "ur"]
consonants = ["þ", "ð", "ç", "š", "tš", "z", "t", "n", "w", "f", "r", "d", "s", "ng"]
vowel = random.randint(0, 53)
vowel2 = random.randint(0, 53)
consonant = random.randint(0, 13)
consonant2 = random.randint(0, 13)
# tssc = ca thssc = can thssv = ate tssv = at
while vowel == 49, 50, 51, 52, 53 and consonant == 10:

tssc = consonants[consonant] + vowels[vowel]
thssc = consonants[consonant] + vowels[vowel] + consonants[consonant2]
thssv = vowels[vowel] + consonants[consonant] + vowels[vowel2]
tssv = vowels[vowel] + consonants[consonant]
capornah = input("cap or nah ")
which1 = input("which syllabley ou want ")
if capornah == "cap":
    if which1 == "tssc":
        print(tssc.capitalize())
    elif which1 == "thssc":
        print(thssc.capitalize())
    elif which1 == "thssv":
        print(thssv.capitalize())
    elif which1 == "tssv":
        print(thssc.capitalize())
    else:
        print("bruh")
elif capornah == "nah":
    if which1 == "tssc":
        print(tssc)
    elif which1 == "thssc":
        print(thssc)
    elif which1 == "thssv":
        print(thssv)
    elif which1 == "tssv":
        print(thssc)
    else:
        print("bruh")
else:
    print("double bruh")

不要修复元音,idc 不管怎样,我做了“虽然元音== 49,50,51,52,53和辅音==10:”这样它就不会生成“ar”作为元音,然后生成“r”。但我不知道如何重新滚动 randint。有谁知道有什么办法吗?

python random
1个回答
0
投票

您可以使用循环

randint()
:

import random

random.seed(1) # Remove the seed. 


def _syl():
    while True:
        v, c = random.randint(0, 53), random.randint(0, 13)
        if v not in {49, 50, 51, 52, 53} or c != 10:
            return consonants[c] + vowels[v]


def _double():
    c1, c2 = random.randint(0, 13), random.randint(0, 13)
    return consonants[c1] + vowels[random.randint(0, 53)] + consonants[c2]


def _vowel():
    v1, v2 = random.randint(0, 53), random.randint(0, 53)
    c = random.randint(0, 13)
    return vowels[v1] + consonants[c] + vowels[v2]


def _cap(w, iscap):
    return w.capitalize() if iscap == "cap" else w


def get_res(syl, iscap):
    syls = {"tssc": _syl, "thssc": _double, "tssv": _double, "thssv": _vowel}

    return _cap(syls[syl](), iscap) if syl in syls else 'bruh'


vowels = ["a", "a", "ä", "e", "e", "ë", "œ", "ø", "i", "i", "ï", "o", "o", "ö", "u", "u", "ü",
          "ă", "ĕ", "ô", "au", "ou", "a", "a", "ä", "e", "e", "ë", "œ", "ø", "i", "i", "ï", "o", "o", "ö", "u", "u", "ü",
          "ă", "ĕ", "ô", "au", "ou", "a", "e", "i", "o", "u", "ar", "er", "ir", "or", "ur"]
consonants = ["þ", "ð", "ç", "š", "tš", "z", "t", "n", "w", "f", "r", "d", "s", "ng"]

print(get_res('tssv', 'cap'))
print(get_res('tssc', 'cap'))
print(get_res('tssv', 'nah'))
print(get_res('thssc', 'nah'))
print(get_res('thssv', 'cap'))


打印

Çirf
Ðu
tšið
sin
Ôsä
© www.soinside.com 2019 - 2024. All rights reserved.