0

I'm something of a newbie to Python coding and I've just been making short games to get into writing code more fluently. I have right now a "simulation" that is essentially a text-based fight between a hero and a goblin. I am using a tuple to store the moves list and then calling on the elements in that tuple in a series of if statements. MY problem is that when the user enters the number 2, the "potion" move is used, but when the user enter 3, the "potion" move is also used. The number 2 should trigger the "block" move, but does not. I think this may have to do with my limited knowledge of tuples, but can anyone clarify this for me? Much appreciated. The code is as follows...

#begins battle loop
while goblin > 0:

    hmoves = ('sword',
             'shield bash',
             'block',
             'potion')

    choice = int(input("\nEnter a number 0 - 3 to choose an attack: "))

    if hmoves[choice] is 'sword':
        print(name, "attacked with his sword!")
        goblin -= 3
        print("\ngoblin used bite!")
        hero -= 2
        print("Goblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'shield bash':
        print(name, "used shield bash!")
        goblin -= 2
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'block':
        print(name, "used block!")
        print("\ngoblin used bite!")
        print("but it was blocked.")
        hero = hero
        goblin = goblin
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] is 'potion':
        print(name, "used a health potion.")
        hero += 4
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)

    #print("Goblin HP:", goblin, "Hero HP:", hero)

if goblin <= 0:
    print("Congratulations you've completed the simulation.")
else:
    print("Sorry, you did not pass the simulation.")
Andy G
  • 19,232
  • 5
  • 47
  • 69
Tom Crews
  • 135
  • 2
  • 12
  • 4
    (1) What happens when you print out `hmoves[choice]`? (2) For string comparison, use `==` instead of `is`. – NPE May 27 '14 at 16:45
  • 2
    first issue I see is you do not want to use `a_var is "SomeString"` chances are it is not doing what you expect... instead use `a_var == "some string"` (hoever it is fine to say `a_var is None` or `a_var is False` or `a_var is True` ... but is is checking the memory address identity – Joran Beasley May 27 '14 at 16:45
  • 1
    Indeed. ``is`` checks for *reference equivalence*, in other words if two variables are the exact same object, *not* two different objects with the same 'value'. ``==`` checks for 'value' equivalence, however that particular object type defines equality. Your code actually works at all out of pure luck; Python caches very short strings for performance reasons, so the ``is`` statement works. But ``==`` is definitely what you meant. – aruisdante May 27 '14 at 16:48
  • oh sweet, yeah the use of is was out of pure luck, didn't even think it would work! Thanks for clearing that up, guys. – Tom Crews May 27 '14 at 16:55

1 Answers1

2

You should change your stuff from is to ==:

goblin = 20
hero = 20
name = "lol"

#begins battle loop
while goblin > 0:

    hmoves = ('sword',
             'shield bash',
             'block',
             'potion')

    choice = int(input("\nEnter a number 0 - 3 to choose an attack: "))

    if hmoves[choice] == 'sword':
        print(name, "attacked with his sword!")
        goblin -= 3
        print("\ngoblin used bite!")
        hero -= 2
        print("Goblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'shield bash':
        print(name, "used shield bash!")
        goblin -= 2
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'block':
        print(name, "used block!")
        print("\ngoblin used bite!")
        print("but it was blocked.")
        hero = hero
        goblin = goblin
        print("\nGoblin HP:", goblin, "Hero HP:", hero)
    elif hmoves[choice] == 'potion':
        print(name, "used a health potion.")
        hero += 4
        print("\ngoblin used bite!")
        hero -= 2
        print("\nGoblin HP:", goblin, "Hero HP:", hero)

Refer to the difference between is and ==. The two strings are not necessarily the same object in memory, but they are same in terms of the characters. It will work sometimes though because of string interning, which is used for efficiency purposes.

Community
  • 1
  • 1
Dair
  • 15,910
  • 9
  • 62
  • 107