简体   繁体   English

类型错误:+ 不支持的操作数类型:'int' 和 'str' 尝试“打印”时

[英]TypeError: unsupported operand type(s) for +: 'int' and 'str' when trying to 'print'

def findLongRepeats(strToSearch):
"""Search strTosearch to find the first location of longest repeated string 
of a single digit e.g '1111'. Do this for each digit from 0 to 9"""
numbers = ['0','1','2','3','4','5','6','7','8','9']
for number in numbers: #reads a number line one at time
    number_count = 0 #sets the count to 0
    number = int(number)
    longrepeats = []  #creates a new list
    for x in strToSearch: #reads each digit one by one
        if x == number: #if x is true it will add 1 to the number count
            number_count += 1
        if x != number: # if x is not flase it will add number count into the long repeat list
            longrepeats.append(number_count)
            number_count = 0
    print "Found", max(longrepeats), number+'\'s in a row at character position', strToSearch.index(number*max(longrepeats))

def main():
    """DocT"""
    File = open("pidigits.txt","rU") #opens file for reading
    Thousand_String = 0
    strLine =''
    for line in File: #reads each line
        line = line.strip()
        if '3.' in line:
            line = line.replace('3.','')
        Thousand_String += len(line)
        strLine += str(line)
        #print line
    #File.close()



    print
    print "Number of pi digits to process:",Thousand_String
    print
    findLongRepeats(strLine)
    print line
main()

"When i run the function findLong Repeats -- get a this error: “当我运行 function findLong Repeats 时——出现此错误:

File "PA5_.py", line 53, in main
    findLongRepeats(strLine)
  **File "PA5_.py", line 18, in findLongRepeats
    print "Found", max(longrepeats), number+'\'s in a row at character position', strToSearch.index(number*max(longrepeats))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I can't figure out how to fix the error please help.我不知道如何解决错误请帮助。

You can't add a string ( '\'s in a row at character position' ) to a number number .您不能将字符串( '\'s in a row at character position' )添加到数字number中。 The proper way to interpolate numbers into strings is to use string formatting not addition:将数字插入字符串的正确方法是使用字符串格式而不是加法:

print "Found {0} {1}'s in a row at character position {3}".format(max(longrepeats), number, strToSearch.index(number * max(longrepeats)))

The proper way is to use String formatting as noted above, here is the documentation: http://docs.python.org/library/string.html#formatstrings正确的方法是如上所述使用字符串格式,这里是文档: http://docs.python.org/library/string.html#formatstrings

Another simple way to solve this issue is not to add using "+", but independently print the int and string variables separated by a comma:解决这个问题的另一种简单方法是不使用“+”添加,而是独立打印以逗号分隔的 int 和 string 变量:

print "Found", max(longrepeats), number,'\'s in a row at character position', strToSearch.index(number*max(longrepeats))

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

相关问题 尝试打印(sum(list))但收到错误类型错误:+:&#39;int&#39;和&#39;str&#39;不受支持的操作数类型 - Trying to print(sum(list)) but getting error TypeError: unsupported operand type(s) for +: 'int' and 'str' 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' typeerror 不支持 / &#39;str&#39; 和 &#39;int&#39; 的操作数类型 - typeerror unsupported operand type(s) for / 'str' and 'int' TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError:unsupported operand type(s) for -: 'str' and 'int' + 不支持的操作数类型:“int”和“str”[TypeError] - unsupported operand type(s) for +: 'int' and 'str' [TypeError] TypeError: 不支持的操作数类型 -: 'str' 和 'int' - TypeError: unsupported operand type(s) for -: 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM