Loops and checking for string starts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Indigestion
    New Member
    • Dec 2009
    • 2

    Loops and checking for string starts

    I've started with Python recently, reading a book.
    Unfortunately, I've come through an error I do now know how to resolve.

    If you could please help me, it would be greatly appreciated.

    Code:
    from random import randrange
    
    #Simulates a fight
    
    monsters = [
        'Goblin',
        'Orc',
        'Human',
        'Soldier',
        'Fighter',
        'Night Elf'
        ]
    
    rnd = randrange(0, 5)
    x = monsters[rnd]
    
    i = 0
    while i <= range(len(monsters)):
        [B]if monsters[i].startswith('a' | 'e' | 'i' | 'u' | 'o'):[/B]
           an = 'an '
           i += 1
        else:
           an = 'a '
           i += 1
    
    print "You are fighting " + an + x
    That is the code, that will (in the future) simulate a fight. At the moment, what I am trying to do is check if the monster starts with a vowel so that instead of "a Orc" it says "an Orc", which is correct.

    I have tried with these ( | ) signs and with the or keyword, but none of them worked.
  • infixum
    New Member
    • Dec 2009
    • 1

    #2
    Loops and checking for string starts

    In Python 2.5 and above, you can use a tuple as an argument to startswith:

    >>> a = 'hello'
    >>> a.startswith((' hel', 'he', 'no'))
    True
    >>>

    Hope this helps.

    Carl T.

    Comment

    • Indigestion
      New Member
      • Dec 2009
      • 2

      #3
      In fact, your answer did help me (however it had nothing to do with my question), because your arguments reminded me somehow of a list, and remembered the possibility of turning a word into a list.

      Then, it was easy to check for this list's first letter, and see if it was a vowel or not.

      For anyone interested, this is the final code:

      Code:
      from random import randrange
      
      #Simulates a fight
      
      monsters = [
          "Goblin",
          "Orc",
          "Human",
          "Soldier",
          "Fighter",
          "Night Elf"
          ]
      
      rnd = randrange(0, 5)
      x = monsters[rnd]
      xList = list(x)
      
      an = 'a '
      if xList[0] == 'a' or 'e' or 'i' or 'o' or 'u': an = 'an '
      
      print "You are fighting " + an + x

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Using the "in" operator would be appropriate in your case.
        Code:
        for monster in monsters: 
            print "You are fighting %s %s" % \
                  (["a", "an"][monster[0].lower() in \
                               ['a', 'e', 'i', 'o', 'u'] or 0], monster)
        Output:
        Code:
        >>> You are fighting a Goblin
        You are fighting an Orc
        You are fighting a Human
        You are fighting a Soldier
        You are fighting a Fighter
        You are fighting a Night Elf
        >>>

        Comment

        Working...