【发布时间】:2020-01-06 20:20:56
【问题描述】:
我正在编写一些代码,其中我读取了以下二进制数:
0000
0001
1000
1001
00000000
0000000
000000
00000
0000
部分代码读入输入使得s = input()。然后我调用函数accepts(s),其定义如下:
def accepts(str_input):
return accept_step(states[0], str_input, 0) # start in q0 at char 0
accept_step函数定义为:
def accept_step(state, inp, pos):
if pos == len(inp): # if no more to read
return state.is_final_state # accept if the reached state is final state
c = inp[pos] # get char
pos += 1
try:
nextStates = state.transitions[c]
except():
return False # no transition, just reject
# At this point, nextStates is an array of 0 or
# more next states. Try each move recursively;
# if it leads to an accepting state return true.
"""
*** Implement your recursive function here, it should read state in nextStates
one by one, and run accept_step() again with different parameters ***
"""
for state in nextStates:
if accept_step(state, inp, pos): #If this returns true (recursive step)
return True
return False # all moves fail, return false
"""
Test whether the NFA accepts the string.
@param in the String to test
@return true if the NFA accepts on some path
"""
我收到此错误:
if pos == len(inp): # if no more to read
TypeError: object of type 'int' has no len()
我已经尝试过使用str(s)(转换),例如input(str(s)) 和accepts(str(s)),但无济于事。
似乎无论出于何种原因,我的输入文本都被读入为整数,而不是字符串。
我想以字符串而不是整数的形式读取我的输入,并且能够使用字符串的len() 属性来执行我的程序。有人可以指出我正确的方向,并向我解释为什么我的输入被读入整数而不是字符串?我想如果我特别想要整数输入,我将不得不使用类似int(input())?
【问题讨论】:
-
我正在使用 python 3.6.9 接收您的二进制代码作为字符串类型。
标签: python string input integer