[英]What am I doing wrong with these while loops that I get stuck on the third iteration?
嘗試在 python 3.9 中實現 FSA,
編輯:
就上下文而言,這是我作為一個學習編碼的人必須做的第一個重大項目。 我遲到了 5 周才被扔進一門大學課程,基本上被告知要盡力趕上。 我很難弄清楚循環機制在做什么,並且 class 與變量結構應該看起來像。 FSA 有問題“在此識別器中,起始 state 是 S1,而 S7 是唯一接受 state。(不接受空字符串)。如果上述 FSA 接受,您的程序應要求用戶輸入字符串並打印 'True'它,否則為“False”。打印結果和“再見”消息后,程序應立即停止。”
我的表格顯示了 FSA 正在做什么:表格
當它檢查輸入的數字時,它應該根據所需的步驟對它們進行測試,並根據它是否滿足要求來向后或向前移動。 對於此示例,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()
我坐下來仔細看了看,然后想出了一個解決方案。 我不確定它是否正確,但它確實運行了我認為是此 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.