首页 > 解决方案 > 我在第三次迭代中卡住的这些 while 循环做错了什么?

问题描述

尝试在 python 3.9 中实现 FSA,

编辑:

就上下文而言,这是我作为一个学习编码的人必须做的第一个重大项目。我已经晚了大约 5 周被扔进了一门大学课程,并且基本上被告知要尝试并尽我所能赶上。我在弄清楚循环机制在做什么以及类与变量结构应该是什么样子时遇到了很多麻烦。 FSA 有问题 “在此识别器中,起始状态是 S1,S7 是唯一的接受状态。(不接受空字符串)。如果上述 FSA 接受,您的程序应要求用户输入字符串并打印 'True'它,否则为“False”。打印结果和“再见”消息后,程序应立即停止。”

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

我也一直在努力将我所有的工作都保存在一个 github 上。

当它检查输入的数字时,它应该根据所需的步骤对它们进行测试,并根据它是否满足要求来向后或向前移动。对于此示例,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()

标签: python

解决方案


我坐下来仔细看了看,然后想出了一个解决方案。我不确定它是否正确,但它确实运行了我认为是此 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))

推荐阅读