自动编程

2024-09-27 09:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要写一个脚本,当琼睡觉,琼的妈妈睡觉,琼的妈妈的表妹睡觉时,生成不同的句子。琼的姐姐的丈夫的哥哥睡觉等等。为此,我编写了一个包含8个函数的代码,如下所示。现在我想写第二步,它将要求输入一个句子使用家庭成员,如“我表弟的母亲睡觉”,并显示采取了哪些步骤来生成这个句子。到目前为止,我掌握的情况如下:

# _*_ coding: utf-8 _*_
import random

def etat7(chaine):
    return chaine+"dort."

def etat6(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="Jean "
        return etat7(chaine+mot)
    elif choix==1:
        mot="la "
        return etat3(chaine+mot)
    elif choix==2:
        mot="l' "
        return etat4(chaine+mot)
def etat5(chaine):
    choix=random.choice(range(0,2))
    if choix==0:
        mot="de "
        return etat6(chaine+mot)
    if choix==1:
        mot="du "
        return etat2(chaine+mot)

def etat4(chaine):
    choix=random.choice(range(0,2))
    if choix==0:
        mot="ancêtre "
        return etat5(chaine+mot)
    elif choix==1:
        mot="oncle "
        return etat5(chaine+mot)

def etat3(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="mère "
        return etat5(chaine+mot)
    elif choix==1:
        mot="soeur "
        return etat5(chaine+mot)
    elif choix==2:
        mot="nièce "
        return etat5(chaine+mot)


def etat2(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="père "
        return etat5(chaine+mot)
    elif choix==1:
        mot="frère "
        return etat5(chaine+mot)
    elif choix==2:
        mot="neveu "
        return etat5(chaine+mot)

def etat1(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="le "
        return etat2(chaine+mot)
    elif choix==1:
        mot="la "
        return etat3(chaine+mot)
    elif choix==2:
        mot="l' "
        return etat4(chaine+mot)

print etat1("")

在这一步,我有一个程序,生成不同的句子,但我不明白如何进行。你知道吗

谢谢


Tags: returnifdefrangerandom句子choiceelif
1条回答
网友
1楼 · 发布于 2024-09-27 09:26:26

获取输出并向后分析

output = etat1("")
d = {"dort.": "etat7",
     "Jean": "etat6",
     "la": "etat6",
     "l'": "etat6",
     "de": "etat5",
     "du": "etat5",
     "ancêtre": "etat4",
     "oncle": "etat4",
     "mère": "etat3",
     "soeur": "etat3",
     "nièce": "etat3",
     "père": "etat2",
     "frère": "etat2",
     "neveu": "etat2"
}

words = output.split()[::-1]
calls = []

for i in words[:-1]:
    calls.append(d[i])
calls.append("etat1")
"->".join(calls[::-1])

相关问题 更多 >

    热门问题