简体   繁体   中英

Why is it that two different numbers return the same string in a tuple?

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.")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM