我在第三次迭代中遇到的while循环有什么问题?

2024-06-16 09:23:36 发布

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

试图在python 3.9中实现FSA

编辑:

在上下文中,这是我作为一个学习编码的人必须做的第一个主要项目。我参加了一个大学课程,大约晚了5个星期,基本上我被告知要尽力赶上。我很难弄清楚循环机制到底在做什么,类和变量结构应该是什么样子。 FSA in question “在此识别器中,开始状态为S1,S7是唯一接受状态。(不接受空字符串)。如果上述FSA接受,您的程序应要求用户输入字符串并打印‘True’,否则为‘False’。打印结果和‘再见’消息后,程序应立即停止。”

我的表格显示了FSA正在做什么:table

I've been trying to keep a github of all my work as well.

当它检查输入的数字时,它应该根据所需的步骤测试它们,并根据是否满足要求向后或向前移动。在本例中,att应作为唯一的成功通过测试

我一直很难让循环正常工作。我可以让前两个发生,但它要么卡在第三个,要么根本不运行

att = 'bbaccb'


class rec:
    a = 'a'
    b = 'b'
    c = 'c'
    i = 0
    x = 1
    cd = str(att[i])

    def fx():
        rec.x = rec.x + 1
        print(rec.i, rec.x, rec.cd)

    def fi():
        rec.i = rec.i + 1
        print(rec.i, rec.x, rec.cd)

    def bx():
        rec.x = rec.x - 1
        print(rec.i, rec.x, rec.cd)

    def sx():
        rec.x = rec.x + 0
        print(rec.i, rec.x, rec.cd)

    def fail():
        print('False','\n','Goodbye')
        quit()

    def pas():
        print('True\n', 'Goodbye\n')
        quit()

    def forward1():
        rec.fx()
        rec.fi()

    def stepback():
        rec.bx()
        rec.fi()

    def stepback2():
        rec.x = rec.x - 2
        rec.fi()

    def standstill():
        rec.sx()
        rec.fi()
    
    def steps(object): 
        while rec.x > 0:
            print(rec.i, rec.x, rec.cd)
            while rec.x == 1:
                print(rec.i, rec.x, rec.cd)
                if rec.cd == rec.b:
                    rec.forward1()
                if rec.cd == rec.c:
                    rec.forward1()
            else:
                rec.fail()
            
                while rec.x == 2:
                    print(rec.i, rec.x, rec.cd)
                    if rec.cd == rec.a:
                        rec.stepback()
                    if rec.cd == rec.b:
                        rec.forward1()
                else:
                    rec.fail()
                    while rec.x == 3:
                        print(rec.i, rec.x, rec.cd)
                        if rec.cd == rec.a:
                            rec.forward1()
                    else:
                        rec.fail()
                        
                        while rec.x == 4:
                            print(rec.i, rec.x, rec.cd)
                            if rec.cd == rec.a:
                                rec.standstill()
                            if rec.cd == rec.b:
                                rec.stepback()
                            if rec.cd == rec.c:
                                rec.forward1()
                        else:
                            rec.fail()
                            while rec.x == 5:
                                print(rec.i, rec.x, rec.cd)
                                if rec.cd == rec.b:
                                    rec.stepback2()
                                if rec.cd == rec.c:
                                    rec.forward1()
                            else:
                                rec.fail()
                                while rec.x == 6:
                                    print(rec.i, rec.x, rec.cd)
                                    if rec.cd == rec.a:
                                        rec.stepback()
                                    if rec.cd == rec.b:
                                        rec.forward()
                                    if rec.cd == rec.c:
                                        rec.standstill()
                                else:
                                    rec.fail()
                                    while rec.x == 7:
                                            print(rec.i, rec.x, rec.cd)
                                            pas()
        else:
            rec.fail()


Tags: if状态defcdelseattfifsa
1条回答
网友
1楼 · 发布于 2024-06-16 09:23:36

我坐下来仔细看了一下,然后想出了一个解决办法。我不确定它是否正确,但它确实运行我认为是这个FSA的“密码”

#==============================================================================
#Variables
#==============================================================================
# Each accepted digit
a = 'a'
b = 'b'
c = 'c'
i = 0
# Placement in attempted string

# Inputed array from the user
arr = input(str('Please enter the string to be recognized: '))

#testing
#arr = 'bbaccb'
#==============================================================================
"""
In this recognizer, the starting state is S1, and S7 is the only accepting state
(empty strings are not accepted). Your program should ask the user to enter a
string and print "True" if the FSA accepts it, "False" otherwise. After printing
the result (and a 'Goodbye' message), the program should immediately stop.
"""

"""
Step 1:
    If arr[i] is an a, fail.
    If b, go to next step with next digit.
    If c, check digit 2
"""
def step1(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        print(false())
    if arr[i] == b:
        forward(object)
        step2(object)
    if arr[i] == c:
        i = i + 1
        step1(object)
    else:
        print(false())
"""
Step 2:
    If arr[i] is an a, return to step 1 with next digit.
    If b, go to next step with next digit.
    If c, fail.
"""
def step2(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        forward(object)
        step1(object)
    if arr[i] == b:
        forward(object)
        step3(object)
    if arr[i] == c:
        print(false())
    else:
        print(false())
"""
Step 3:
    If arr[i] is an a, go to next step with next digit.
    If b, fail.
    If c, fail.
"""
def step3(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        forward(object)
        step4(object)
    if arr[i] == b or c:
        print(false())
    else:
        print(false())
"""
Step 4:
    If arr[i] is an a, check the next digit.
    If b, return to step 3 with the next digit.
    If c, go to next step with next digit.
"""
def step4(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        forward(object)
        step4(object)
    if arr[i] == b:
        forward(object)
        step3(object)
    if arr[i] == c:
        forward(object)
        step5(object)
    else:
        print(false())
"""
Step 5:
    If arr[i] is an a, fail.
    If b, return to step 3 with the next digit.
    If c, go to next step with next digit.
"""
def step5(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        print(fail())
    if arr[i] == b:
        forward(object)
        step3(object)
    if arr[i] == c:
        forward(object)
        step6(object)
    else:
        print(false())
"""
Step 6:
    If arr[i] is an a, return to step 5 with next digit.
    If b, complete task.
    If c, check next digit.
"""
def step6(object):
    global i
    #print(arr[i])
    if arr[i] == a:
        forward(object)
        step5(object)
    if arr[i] == b:
        print(true())
    if arr[i] == c:
        step6(object)
    else:
        print(false())

#If the program compleates the task, then it's 'True'.
def true():
    #print(arr[i])
    print('True.')
    print('Goodbye.')
    quit()

# Otherwise, the arr is 'False'.
def false():
    #print(arr[i])
    print('False.')
    print('Goodbye.')
    quit()

# Move to next item in string
def forward(object):
    global i
    i = i + 1
    #return i
#==============================================================================
# To Do
#==============================================================================
print(step1(arr))

相关问题 更多 >