Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • AB Check Python
    I can't work out what I've done wrong. My code works when there is one occurrence of "a" but if there is 2 "a"s with the first "a" not having an accompanying "b" 3 chars later and one with the "b" it doesn't work. Here is the code:
    def ABCheck(string): 
    # code goes here
      for i in string:
        if (i.lower() == "a") and ((string.index(i) + 4) < len(string)) and \
          (string[string.index(i) + 4].lower() == "b"):
          return "true"
      return "false"
    
    # keep this function call here  
    # to see how to enter arguments in Python scroll down
    print ABCheck(raw_input())  
    
    Sorry if the question was a bit confusing, its rather late here and I'm pretty tired. Thanks in advance for any help!
    chillnz posted this question on 4/22/14 | python
    Answers
  • +
  • 3
  • -
  • Hey, When you use string.index(i) in python is returns the value for the first position of i. So, it will always return the value of the first 'a'. string.index(i) doesn't take in account the for loop. You can use x.index(i) with a start postion. For example, 'aba'.index('a',1) will return 2 because it started from the 1 postion and searched forward from there. I hope this helps. Try looking up the document for .index() and how it works if my explanation wasn't clear.
  • +
  • 0
  • -
  • i don't know my code couldn't run the message taht i get is <IndentationError: unindent does not match any outer indentation level> someone can help me
  • +
  • 0
  • -
  • def LetterChanges(str): lista=["a","e","i","o","u"] newstr="" for el in str: j=0 while j<len(str): if el == lista[j]: newstr.append(el.upper()) else: if el == "z": newstr.append("A") else: newstr.append(el+1) j=j+1 return newstr # keep this function call here # to see how to enter arguments in Python scroll down print LetterChanges(raw_input())
    Log in to write an answer.