Python 我在第三次迭代中遇到的while循环有什么问题?
试图在python 3.9中实现FSA 编辑: 在上下文中,这是我作为一个学习编码的人必须做的第一个主要项目。我参加了一个大学课程,大约晚了5个星期,基本上我被告知要尽力赶上。我很难弄清楚循环机制到底在做什么,类和变量结构应该是什么样子。 “在此识别器中,开始状态为S1,S7是唯一接受状态。(不接受空字符串)。如果上述FSA接受,您的程序应要求用户输入字符串并打印‘True’,否则为‘False’。打印结果和‘再见’消息后,程序应立即停止。” 我的表格显示了FSA正在做什么: 当它检查输入的数字时,它应该根据所需的步骤测试它们,并根据是否满足要求向后或向前移动。在本例中,att应作为唯一的成功通过测试 我一直很难让循环正常工作。我可以让前两个发生,但它要么卡在第三个,要么根本不运行Python 我在第三次迭代中遇到的while循环有什么问题?,python,Python,试图在python 3.9中实现FSA 编辑: 在上下文中,这是我作为一个学习编码的人必须做的第一个主要项目。我参加了一个大学课程,大约晚了5个星期,基本上我被告知要尽力赶上。我很难弄清楚循环机制到底在做什么,类和变量结构应该是什么样子。 “在此识别器中,开始状态为S1,S7是唯一接受状态。(不接受空字符串)。如果上述FSA接受,您的程序应要求用户输入字符串并打印‘True’,否则为‘False’。打印结果和‘再见’消息后,程序应立即停止。” 我的表格显示了FSA正在做什么: 当它检查输入的数
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()
我坐下来仔细看了一下,然后想出了一个解决办法。我不确定它是否正确,但它确实运行我认为是这个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))
你到底想干什么?实际上,您没有理由需要将循环嵌套得如此之深。您不需要
else
。代码不会被执行的唯一方法是,如果您使用了break
语句,并且您从来没有这样做过。您的fail()
函数会立即退出程序,因此当rec.x==1循环时,它永远不会超出。这段代码简直是洛夫克拉夫特式的恐怖,我敢肯定它应该做的任何事情都可以用更短的方式实现,但是没有一个变量/函数有有意义的名称,也没有任何注释给出任何关于意图的线索。根据您的解释,不清楚您为什么要使用FSA,而不是仅仅检查==“bbaccb”
@Samwise?