0
def is_double_char(str):
  end = len(str)+1
  print(type(end))
  for i in range(1, len(str)): 
    add_one = i+1
    print(type(i))
    print(type(add_one))
    if  str[add_one, end].find(str[i]) != -1:
        return True
  return False

This is the code that I have. The method should find if the string contains 2 or more of the same character.

print(is_double_char("hello"))
______________________________
<class 'int'>
<class 'int'>
<class 'int'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-606a7223e550> in <module>()
----> 1 print(is_double_char("hello"))

<ipython-input-20-b1c815934cad> in is_double_char(str)
      6     print(type(i))
      7     print(type(add_one))
----> 8     if  str[add_one, end].find(str[i]) != -1:
      9         return True
     10   return False

TypeError: string indices must be integers

I don't understand. From my debug prints, all of my indices are already integers. Can someone please help? Thank you.

2 Answers2

0

Code:

def is_double_char(str):
    end = len(str)+1
    for i in range(1, len(str)): 
        add_one = i+1
        if  str[add_one:end].find(str[i]) != -1:
            return True
    return False
    
print(f'hello: {is_double_char("hello")}')
print(f'helo: {is_double_char("helo")}')

Output:

hello: True
helo: False

The only modification I made was the way you were splicing the list. Also, using str as name of input parameter is a very bad practice. Try using something like str_input.

Harshit Jindal
  • 621
  • 8
  • 26
-1

Maybe this is what you need:

def is_double_char(string):
  end = len(string)+1
  print(type(end))
  for i in range(1, len(string)): 
    add_one = i+1
    print(type(i))
    print(type(add_one))
    if  string[add_one: end].find(string[i]) != -1:
        return True
  return False

Note: I changed the variable named str with string because str is a built-in type.

I replaced , with : (line 8).

SAL
  • 547
  • 2
  • 8
  • 25