Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动机编程_Python_Automata - Fatal编程技术网

Python 自动机编程

Python 自动机编程,python,automata,Python,Automata,我需要写一个脚本,当琼睡觉时,琼的母亲睡觉时,琼的母亲的表弟睡觉时,我会写出不同的句子。琼的姐姐的丈夫的哥哥睡觉等等。为此,我编写了一个包含八个函数的代码,如下所示。现在我想写第二步,要求输入一个句子,使用家庭成员,如“我表妹的妈妈睡觉”,并显示生成句子所采取的步骤。以下是我到目前为止的情况: # _*_ coding: utf-8 _*_ import random def etat7(chaine): return chaine+"dort." def etat6(chaine)

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

# _*_ 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("")
在这一步中,我有一个生成不同句子的程序,但我不知道如何继续

谢谢

获取输出并向后解析

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])

我不会说法语,所以我很难读懂这个。您的代码结构也非常不干净,因为您复制了很多代码。在添加任何其他内容之前,您肯定应该尝试简化代码。