[英]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. 我是Python编码的新手,我一直在做一些简短的游戏,以便更流畅地编写代码。 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.
我使用一个元组存储移动列表,然后在一系列if语句中调用该元组中的元素。 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.
我的问题是,当用户输入数字2时,将使用“药水”移动,但是当用户输入3时,还将使用“药水”移动。 The number 2 should trigger the "block" move, but does not.
数字2应该触发“阻止”移动,但不会触发。 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 ==
: 您应该将东西从
is
更改为==
:
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 ==. 请参阅is和==之间的区别。 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.
尽管有时由于有效的字符串interning ,它仍然可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.