简体   繁体   English

在Python中将字符串读取为整数吗?

[英]Strings being read in as integers in Python?

I'm working on some code in which I read in the following binary numbers: 我正在编写一些代码,其中读取了以下二进制数字:

0000
0001
1000
1001
00000000
0000000
000000
00000
0000

And part of the code reads in input such that s = input() . 部分代码读入input,使s = input() I then call the function accepts(s) , which has the following definition: 然后,我将函数定义为具有以下定义的accepts(s)

def accepts(str_input):
    return accept_step(states[0], str_input, 0)  # start in q0 at char 0

The accept_step function is defined as: 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
"""

I get this error: 我收到此错误:

    if pos == len(inp):  # if no more to read
TypeError: object of type 'int' has no len()

I have already tried using str(s) (conversion), such as in input(str(s)) and accepts(str(s)) , but to no avail. 我已经尝试过使用str(s) (转换),例如在input(str(s))accepts(str(s)) ,但无济于事。

It seems that for whatever reason, my input text is being read in as integers, and not as strings. 看来,无论出于什么原因,我的输入文本都以整数而不是字符串的形式读取。

I would like to read my input in as strings and not integers, and be able to use the len() property of strings to execute my program. 我想以字符串而不是整数的形式读取我的输入,并能够使用字符串的len()属性执行我的程序。 Could someone please point me in the right direction, and explain to me why my input is read in as integers and not strings? 有人可以指出正确的方向,并向我解释为什么我的输入是整数而不是字符串吗? I thought if I specifically wanted integer input, I would have to use something like int(input()) ? 我以为如果我特别想要整数输入,就不得不使用int(input())吗?

Python attempts to assume the type of the variable that is input. Python尝试假定输入变量的类型。 In this case it believes that you are entering integers. 在这种情况下,它认为您正在输入整数。 So try str() around the input during assignment. 因此,在分配期间尝试在输入周围使用str()。

s = str(input())
accepts(s)

For example some tests in Python3: 例如,Python3中的一些测试:

>>> a = 1001
>>> isinstance(a, int)
Returns: True

>>> b = '1001'
>>> isinstance(b, int)
Returns: False

>>> c = str(1001)

>>> isinstance(c, int)
Returns: False

>>> isinstance(c, str)
Returns: True

>>> len(c)
Returns: 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM